4

I use attribute routing, but when I specify an empty Route attribute I get the following error:

405.0 - Method Not Allowed

However, if I add a route name in the attribute, like [Route("bar")], everything works as expected.

Why would one of these action methods work as expected, while the other one yields a 405 error?

[System.Web.Http.RoutePrefix("foo")]
public partial class MyController : ApiController
{
   [System.Web.Http.HttpPost]
   [System.Web.Http.Route("bar")] // I am able to POST to /foo/bar
   public async Task<MyResponseModel> BarMethod([FromBody]MyArgumentsModel arguments)
   {

   }

   [System.Web.Http.HttpPost]
   [System.Web.Http.Route] // Error when I POST to /foo, "Method Not Allowed"
   public async Task<MyResponseModel> FooMethod([FromBody]MyArgumentsModel arguments)
   {

   }
}

Any ideas what I could be missing?

1
  • You don't need to use [System.Web.Http.Route]. Just remove it and the method will inherit the controllers route. Commented Sep 20, 2016 at 10:52

2 Answers 2

2

You need to include an empty string to the route attribute [Route("")] in order for it to work as the default route when using the route prefix.

The following article shows how it is done

Source: Attribute Routing in ASP.NET Web API 2

The result of the suggested change would look like this

[RoutePrefix("foo")]
public partial class MyController : ApiController {
   //eg POST foo/bar
   [HttpPost]
   [Route("bar")]
   public async Task<MyResponseModel> BarMethod([FromBody]MyArgumentsModel arguments) {
      //...
   }

   //eg POST foo    
   [HttpPost]
   [Route("")]
   public async Task<MyResponseModel> FooMethod([FromBody]MyArgumentsModel arguments) {
       //...
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help! However, adding an empty string yields the same result. :/
Check the examples in the article liked in the answer.
1

Attribute routing can be combined with convention-based routing. To define convention-based routes, call the MapHttpRoute method.

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 } );
    }
}

6 Comments

Thanks for helping! Although I already have that exact configuration in place. :)
@TedNyberg Then try to rename FooMethod to Post
I tried that before, but it made no difference. :/ Thanks though!
@TedNyberg i have an application online that currently works with this configuration/ attributes... what version of webapi/ framework are you using?
Version 5.2.3. Weird thing is, this has worked before. :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.