Yecats
Yecats

Reputation: 1815

Create a REST API in ASP.Net

I have an ASP .Net web application that I have created in Visual Studio. I was following the KendoUI tutorial located here for creating a REST API. I believe that I have set it up properly but the problem is that when I navigate to the URL it says it can't find it.

This is my controller:

    public class IngredientControllers : ApiController
    {
        private Data.RecipeTrackerDataContext _context = new Data.RecipeTrackerDataContext();


        // GET api/<controller>/5
        public List<Models.Ingredient> Get()
        {
            var ingredients = from e in _context.Ingredients
                              select new Models.Ingredient
                              {
                                  Id = e.IngredientID,
                                  ItemID = e.ItemID,
                                  Amount = e.Amount
                              };


            return ingredients.ToList();
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}

This is my Global.aspx file:

    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();
            RouteTable.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = System.Web.Http.RouteParameter.Optional });
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }
    }
}

Upvotes: 0

Views: 3129

Answers (1)

Yecats
Yecats

Reputation: 1815

Takepara provided the solution - My cs file should be 'IngredientController' and not 'IngredientControllers.

Upvotes: 1

Related Questions