0

I have the following route configured in my ASP.NET Web API 2 Project:

        config.Routes.MapHttpRoute(
            name: "1MandatoryStringParameter",
            routeTemplate: "api/{controller}/{data}",
            defaults: null,
            constraints: new { data = @".+?" }

It is used with the following controller method (notice the ArrayInput attribute):

[ArrayInput("data",Separator = ',')]
public async Task<IHttpActionResult> Get(int[] data)
{
...
}

I would like to use Attribute routing instead.

I tried to replace the call to MapHttpRoute with the following attributes:

[HttpGet]
[Route("api/ActionsForItemTypesList/{data:regex(.+?)}", Name = "1MandatoryStringParameter")]

Out of the box it does not work. I can't reach my method with an URL like:

api/ActionsForItemTypesList/1,2

If get a 404 Method not found.

This is working fine with route configuration.

Any help appreciated.

EDIT : fixed the client URL.

EDIT 2 : This is an ApiExplorer Issue (Swashbuckle leverage ApiExplorer)

If I modify the Route attribute and remove the parameter (ie. {data}) the ApiDescription becomes available.

Community
  • 1
  • 1
  • Two things. You can create a custom route constraint and a custom model binder for the int array. [Attribute Routing in ASP.NET Web API 2](https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2) – Nkosi Jan 23 '17 at 12:22

1 Answers1

0

Make sure you have enabled attribute routing:

config.MapHttpAttributeRoutes();

Also in your Route attribute you have specified that the data parameter must be part of the path and not a query string. So make sure that you are calling the action correctly from the client:

api/ActionsForItemTypesList/1,2

Also notice that the prefix that you indicated in your Route attribute is api/ActionsForItemTypesList and not api/Controller like you were trying to invoke it.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Attribute routing is enabled and the call from the client is done like you say. For what its worth, I'm using Swashbuckle and Route Debugger. With attribute routing, the method is missing from the swagger doc. –  Jan 23 '17 at 10:26
  • I have just created a new Web API project, removed global routing, enabled attribute routing, decorated the the controller action with this attribute and it worked without any issues. Try doing it in a clean project and progressively add stuff until you find what is causing the issue. – Darin Dimitrov Jan 23 '17 at 10:29
  • URL fixed. For now I'm using a mixture of global and attribute routing. My goal is to only use attribute routing. –  Jan 23 '17 at 10:33
  • Can you try disabling global routing to see if somehow it might be interfering with the attribute route? In theory both should work together but in practice it is not recommended to mix them. – Darin Dimitrov Jan 23 '17 at 10:34
  • I created a new Web API project, removed the default global route, enabled attribute routing, decorated the conroller action, then added the swashbuckle Nuget. Same result, the action is missing from the swagger doc. –  Jan 23 '17 at 13:25
  • Before adding the Swagger NuGet are you able to invoke the controller action? – Darin Dimitrov Jan 23 '17 at 13:26
  • I'm indeed able to invoke the action using fiddler / Route debugger with or without Swashbuckle. I was misled by swagger that seems to fail discovering the action. –  Jan 23 '17 at 13:55
  • Yes, your question was about Web API and attribute based routing initially. You may consider closing this thread and opening a new one specifically for Web API and Swagger integration. – Darin Dimitrov Jan 23 '17 at 14:10