Web API 2 Route Attribute not working?

833 Views Asked by At

OK I have spent all day looking over this code and cannot figure out what I have done wrong.

This is Web API 2 on .NET 4.5.2 Here is my Global.asax.cs

public class WebApiApplication : HttpApplication
{
    protected void Application_Start()
    {
        new AutoFacContainer(GlobalConfiguration.Configuration, Assembly.GetExecutingAssembly());
        GlobalConfiguration.Configuration.Services.Replace(typeof (IHttpControllerSelector),
            new VersionHttpControllerSelector(GlobalConfiguration.Configuration));
        GlobalConfiguration.Configure(WebApiConfig.Register);
        GlobalConfiguration.Configure(WebApiFilter.Register);
    }
}
  • I am using AutoFac for DI, that is working.
  • I have overwritten the ControllerSelector to allow for versioning via sub-folders in the Controller folder. << Yes this is actually working
  • I am then calling to do the Route Registry (see below)
  • Finally I am calling to add some custom filters, there are also working.

Here is my WebApi.Config

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

    }
}

I only want to use the Route Attributes so I do not have any manual route's configured.

Finally here is my 'Default Controller'

[RoutePrefix("test")]
public class DefaultController : WebApiBaseController
{
    [HttpGet]
    [Route("")]
    public virtual IHttpActionResult GetRoot()
    {
        return Ok("This is Version1 of GetRoot");
    }

    [HttpGet]
    [Route("{test/{id:int}", Order = 1)]
    public virtual IHttpActionResult GetTest(int id)
    {
        return Ok("This is Test1 in Version1");
    }
}

Here is the results I get: Root Call Test Action Call If I add the manual Route back in, it work but that means the Route Attribute is not working still. So what have I missed??

0

There are 0 best solutions below