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.