0
votes

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.

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 - Nkosi

1 Answers

0
votes

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.