Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net MVC/Web Api Routing: Need to route a little different

I have setup a asp.net web api project (it works exactly the same as a Mvc Project) using routing - hence i have the following

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

So everything works just the way i want it .... I enter api/Products/15 it it arrives in my Products Controller passing in 15 as the id.

Great.

I also have 2 controllers more, 1 called UploadsController and 1 called DownloadsController. These offer Uploads and Downloads (GET / PUT etc)

Now i don't want them to be picked up by the original rule (see above)

But what i would like is to use these 2 urls to access them

/api/Transport/Uploads/15 /api/Transport/Downloads/15

I have passed in 15 as the ID, probably wouldn't happen in real life... just its good for demonstration :-)

Now Transport doesn't exist so i could do the following

routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/Transports/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

which i believe works...

but the problem is that if i do

/api/Uploads/15 - this would also be caught by the original rule which i don't want..

I want the Uploads and DOwnloads controller to be accessed through the fake "Transports" and not without the Transports

Can anyone help?

Thanks in advance

like image 684
Martin Avatar asked Jun 20 '12 14:06

Martin


People also ask

What makes it different a Web API routing from MVC routing?

If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action. You can also use MVC-style routing in Web API.

How do I specify a route in Web API?

The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config. MapHttpAttributeRoutes() method.

Can we have multiple routes in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.


1 Answers

You could use route constraints and define 2 API routes in the following order:

// matches /api/transports/downloads/id or /api/transports/uploads/id only
// and no other possible controller
routes.MapHttpRoute(
    name: "API Transport",
    routeTemplate: "api/transports/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional },
    constraints: new { controller = "uploads|downloads" }
);

// matches anything in the form /api/{controller}/id where {controller}
// could be anythnig different than "downloads" or "uploads" because we don't
// want to allow the following urls: /api/downloads/id and /api/uploads/id
routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional },
    constraints: new { controller = @"^((?!(downloads|uploads)).)*$" }
);
like image 121
Darin Dimitrov Avatar answered Sep 30 '22 17:09

Darin Dimitrov