Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly handling nested resources in ASP.net MVC 4 WebApi routing

I'd like to provide REST API in this way:

GET /api/devices
POST /api/devices
PUT /api/devices/1
DELETE /api/devices/1

This is my configuration:

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

And these are the actions:

public IEnumerable<Device> Get()
{
//return all devices
}

public Devices Get(id)
{
//return a specific devices
}

and so on.

The issue appears when I want to handle nested resources:

GET /api/devices/1/readings
POST /api/devices/1/readings
GET /api/devices/1/readings/1
PUT /api/devices/1/readings/1
DELETE /api/devices/1/readings/1

This is my configration for these:

config.Routes.MapHttpRoute(
    name: "NestedApi",
    routeTemplate: "api/{controller}/{parentResourceId}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

The issue shows up when trying to GET and POST to the nested resource:

[HttpGet]
public String Readings(int parentResourceId)
{
   //return a list of readings for the device
}

[HttpPost]
public String Readings(int parentResourceId)
{
    //create and return the id of a reading for the device
}

This is, of course, failing because there are two actions with the same signature.

I'd like to hear from a way of accomplishing this with the most RESTful approach

like image 517
martincho Avatar asked Jul 05 '13 16:07

martincho


People also ask

What is the difference between Web API routing and 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 can add multiple route in ASP.NET MVC?

Multiple Routes You can also configure a custom route using the MapRoute extension method. 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.

Which method is generally used for registering Web API routes?

Attribute routing is supported in Web API 2. As the name implies, attribute routing uses [Route()] attribute to define routes. 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.

Which of the following methods is required to enable MVC routing for ASP.NET Core?

MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default . MVC configures the default route template as {controller=Home}/{action=Index}/{id?} .


1 Answers

Microsoft is adding Attribute Routing to increase the flexibility of the routing system. Have a look at their documentation on Scenario 3

There is also some answers on Stack Overflow like:

How to handle hierarchical routes in ASP.NET Web API?

like image 182
oeoren Avatar answered Oct 22 '22 09:10

oeoren