Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI Attribute Routing Not Working in OWIN

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.

like image 210
Graham Avatar asked Jan 25 '26 11:01

Graham


1 Answers

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);
}
like image 67
Nkosi Avatar answered Jan 26 '26 23:01

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!