Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 4 MVC Web API: Documentation for complex routing

The web api seems to be only suited for the standard use-cases. But I want to do more complex routing but can't find documentation for complex routing. If I have more controller, the routings gets more and more complicated.

Can i define several optional parameters with dependencies? Like this:

/api/document/{par1}/{par2}

par1 & par2 should be optional but par2 should be only matched if par1 is present.

And are recursive parameters possible?

/api/documents/characteristics/{round/green/red-dots}
/api/documents/characteristics/{square/yellow}
/api/documents/characteristics/{square/yellow/flat}
/api/documents/characteristics/{square/yellow/flat/...}

Is there a detailed documentation for the web api routing? The microsoft tutorial is too basic... I need more information about the routing.

I have two controllers and some trouble because two routings are quite similar, so the wrong route is taken. I can use [Action]-Attribute as a workaround, but this feels not right... I also have to consider the order of the routes. This is logical but is nowhere mentioned. Is the web api only for simple rest api's?


Edit: I tried this:

routes.MapHttpRoute(
           name: "DefaultApi",
            routeTemplate: "api/{mandant}/documents/{id}",
            defaults: new { controller = "documents", id = RouteParameter.Optional }
            );

//routes.MapHttpRoute(
//    name: "DefaultApiWithAction",
//    routeTemplate: "api/{mandant}/documents/{id}/{action}",
//    defaults: new { controller = "documents" }
//    );

I have two methods:

[AcceptVerbs("get")]
public HttpResponseMessage Get(int id)

[ActionName("file")]
[AcceptVerbs("get")]
public HttpResponseMessage filedownload(int id)

Now I have the problem, the file-action is triggered even if I comment out the second route and the normal get specific document method is not triggered because multiple actions... I tried the [NoAction] attribute but this is not working... But why will the file-method be triggered if there is no action in the route-template? (Or if the second route is active, why will the normal get-document method not be triggered if there is no action in the url....) I my current workaround is to set a default-action for all other methods, but this is not a good solution.

like image 349
user437899 Avatar asked Jun 12 '12 08:06

user437899


1 Answers

You can use routing constraints to set conditions on the routing in global.asax like:

routes.MapRoute(
    "ApiRoute",
    "api/document/{par1}/{par2}",
    new {controller="Document", action="SomeMethod"},
    new {par1 = @"\d+" }
 );

In the last parameter you can specify regular expression that has to be matched for specified parameter for the route to be used. In the example above par1 is used for digits only, but you can use any regular expression, like:

routes.MapRoute(
    "ApiRoute",
    "api/document/{par1}/{par2}",
    new {controller="Document", action="SomeMethod"},
    new {par1 = @"(value1|value2|value3)" }
 );
like image 138
PanJanek Avatar answered Sep 29 '22 04:09

PanJanek