3

I have an issue with WebApi throwing 404 errors for my routes. An example of a controller is:

[System.Web.Http.RoutePrefix("test/categories")]
public class CategoriesController : ControllerBase
{

    [System.Web.Http.Route("hello")]
    [HttpGet]
    [AcceptVerbs("GET")]
    public IHttpActionResult Get([FromUri] QueryOptions options)
    {
        // Usual controller stuff happens here
    }

    [System.Web.Http.Route("{id:int}")]
    [HttpGet]
    [AcceptVerbs("GET")]
    public IHttpActionResult GetById(int id)
    {
        // Usual controller stuff happens here
    }
}

GET requests to https://api.testdomain.com/test/categories/hello or https://api.testdomain.com/test/categories/1 both throw 404 Not Found errors. The app is running on IIS (not IIS Express). The 404 is an IIS 404, which indicates that the app is being started, but the routing is failing. I've attached breakpoints in startup.cs and it is definitely being hit.

My startup.cs file contains:

    public void Configuration(IAppBuilder app)
    {            
        var config = new HttpConfiguration
        {
            IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always,
            DependencyResolver = new SimpleInjectorWebApiDependencyResolver(_container) // _container is a SimpleInjector IoC container; shouldn't affect things.
        };

        WebApiConfig.Register(config);

        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(config); // Have tried commenting this out with no luck
    }

My global.asax contains:

    protected void Application_Start(object sender, EventArgs e)
    {
        GlobalConfiguration.Configure(WebApiConfig.Register); // Have also tried commenting this out with no luck
    }

WebApiConfig.cs looks like this:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Formatters.Add(new FilePropertyInfoMediaTypeFormatter());

    }
}

I have the following packages installed:

<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />

I have no idea why the routing isn't working. It is code ported from other projects, and the approach (i.e. the code in startup.cs etc) is the same as the working projects.

I'm going round in circles so any help gratefully received.

Graham
  • 1,497
  • 2
  • 16
  • 36
  • Where is the default route in your `Register` method ? Some thing like `config.Routes.MapHttpRoute(name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); ` – Ravi A. Nov 25 '16 at 10:13
  • Do you have the ExtensionlessUrlHandler added to your handlers section in web.config? Here's an example: http://stackoverflow.com/questions/9703090/http-404-page-not-found-in-web-api-hosted-in-iis-7-5 – Schadensbegrenzer Nov 25 '16 at 10:15
  • @RaviA. It's using attribute routing, so the default route as you have written is not needed – Graham Nov 25 '16 at 10:22
  • My bad didn't see the URLs closely. So this works in Visual Studio with IIS Express as expected ? Also can you try removing this line `WebApiConfig.Register(config); ` from startup.cs since you are already doing that in `global.asax` – Ravi A. Nov 25 '16 at 10:24
  • @Schadensbegrenzer The ExtensionlessUrlHandler is added already – Graham Nov 25 '16 at 10:47
  • btw... You are using "HttpGet" and "[AcceptVerbs("GET")]" attribute. You just need the first one. Use AcceptVerbs if you are planning to allow more than one verb for a controller method – Schadensbegrenzer Nov 25 '16 at 11:24
  • Just a heads up.. Your global.asax file isnt required. Your using the OWIN middleware to configure the Global HttpConfiguration in your Startup File, so in effect your running the same thing twice. Put a break point in your code.. its being called twice from what i can see here. – Derek Nov 28 '16 at 09:02

1 Answers1

2

You are mixing up the configuration of the HttpConfiguration object with IIS and OWIN. It is either one or the other. using both will cause conflict.

WebApiConfig.cs

public static class WebApiConfig {
    public static void Register(HttpConfiguration config) {
        config.MapHttpAttributeRoutes();

        config.Formatters.Add(new FilePropertyInfoMediaTypeFormatter());

        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

        config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(_container);
    }
}

Startup.cs

public class Startup {
    public void Configuration(IAppBuilder app) {  
        app.UseCors(CorsOptions.AllowAll);
    }
}

Global.asax remains the same

protected void Application_Start(object sender, EventArgs e) {
    GlobalConfiguration.Configure(WebApiConfig.Register);
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472