Request Parameter Route Limitations

I just started using ASP.NET Web API 2.1 and ran into a limitation. Using Attribute Routing, I can do the following:

[Route("item/{id:int}")]
public IHttpActionResult GetItem(int id)
{
    ...
}

The URL /item/5will be redirected to this action, but the URL /item/abcwill not be due to the restriction intin {id:int}.

I tried to change my URL so that the parameter idwas in the query string along with its restriction, despite the use of route restrictions on query parameters that are never mentioned or shown in the documentation.

[Route("item?{id:int}")]
public IHttpActionResult GetItem(int id)
{
    ...
}

If I try to start now, I get an error when calling method Configurein Application_Start.

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

The message is as follows.

ArgumentException was not handled by user code

'/' '~', '?' .

.

-, , MSDN, , ~ , , .

-, , ? , .

[Route("item")]
public IHttpActionResult GetItem(int id)
{
    ...
}

URL- /item/5 id 5 - URL /item/abc, id 0.

?

+4
1

http://attributerouting.net/#asp-net-web-api (& dagger; ) :

" ! - Web API WebHost :...

  • querystring,..."

& dagger;) , -API, AttributeRouting.WebApi NuGet. ​​-API. , , - .

+8

Source: https://habr.com/ru/post/1525510/


All Articles