Search code examples
c#asp.netasp.net-mvcasp.net-web-apiasp.net-mvc-routing

Setting up a WebAPI, Fiddler response is an html page


I am trying to get a web api set up that will use RESTful services. I am following this guide.

Getting Started with ASP.NET Web API 2 (C#)

I am also following this guide for setting up Entity Framework.

Getting Started with Entity Framework 6 Code First using MVC 5

When I run a Composer in Fiddler. I get the webpage for Home.aspx

Here is the code for my controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebServer.z_Repository;
using WebServer.z_Models;

namespace WebServer.z_Controllers
{
    [Route("api/Locations")]
    public class LocationsController : ApiController
    {
        // GET api/<controller>
        static IlocationsRepository LocationsRepo;

        public LocationsController(IlocationsRepository _repo)
        {
            if (_repo == null) { throw new ArgumentNullException("_repo"); }
            LocationsRepo = _repo;
        }

        [HttpGet]
        public IEnumerable<Location> GetAll()
        {
            return LocationsRepo.GetAll();
        }
    }
}

I put a breakpoint on the GetAll() and that breakpoint was never hit. This tells me that the controller isn't registered somewhere. But the guide doesn't say anything about where it should be registered.

I created a Global.asax.cs page even though this is not in the guide. But I am unsure of where to go from here.

Code for Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace WebServer
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }
    }
}

Here is a snippet showing the response in the Fiddler Fiddler showing Home.aspx webpage


Solution

  • Home is showing because according to the URL you showed in Fiddler:

    GET Home.aspx/api/locations
    

    it is being told to go to Home.aspx

    You are using attribute routing but have not shown any setup for it.

    Reference: Attribute Routing in ASP.NET Web API 2

    Your controller should be:

    [RoutePrefix("api/Locations")]
    public class LocationsController : ApiController
    {
        IlocationsRepository locationsRepo;
    
        public LocationsController(IlocationsRepository _repo)
        {
            if (_repo == null) { throw new ArgumentNullException("_repo"); }
            this.locationsRepo = _repo;
        }
    
        //GET api/locations
        [HttpGet]
        [Route(""}]
        public IEnumerable<Location> GetAll()
        {
            return locationsRepo.GetAll();
        }
    }
    

    Your WebApiConfig.cs:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Attribute routing.
            config.MapHttpAttributeRoutes();
    
            // Convention-based routing.
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
    

    and in your global, include

    protected void Application_Start()
    {
        // Pass a delegate to the Configure method.
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
    

    Now in order to hit locations web api you will need to call

    GET api/location
    Host: localhost:59104
    

    which works out to http://localhost:59104/api/locations