The restorative legacy of MVC Web Api

I create myself asp.net mvc 4 web api

I went through the Microsoft videos and I think they are good. The verbs Http are used. Resources are nouns, these are dandies with repositories, etc.

But one thing really scares me

I get the impression that this GET http://www.myurl.com/api/sellers/{id}/products is calm and the GET http://www.myurl.com/api/products?$filter = sellerID eq {id} - no.

All I see in what I read about the new web api is <previous <previous <.

Is there any built-in support for the last former? If not, is there a way to do this without routing everything ?


Edit -
I am looking for something that will allow GET http://www.myurl.com/api/sellers/{id}/products and GET http://www.myurl.com/api/products , etc.


Update:
This question does not make sense for a while, I fixed it, but why the first answer is not marked as correct.


Update:
After talking with MilkyWayJoe, we talked about converting /Seller/2/Products to /Products?$filter = sellerID eq 2 . Has anyone understood how to do this?

+6
source share
3 answers

The url http://www.myurl.com/api/products?$filter= sellerID eq {id} uses OData filters, which are consumed out of the box in the web API when your controller returns the collection as IQuerable . The default value in VS 2012 is currently set to IEnumerable , which does not support OData.

Read more about it here and here .

+3
source

There may be nested resources (at least in the construction of URLs), but there is not much support in MVC; you will have to do most of the work yourself.

First, you need to create a route to work with your structure:

 url: "sellers/{sellerId}/products", defaults: new { controller = "sellers", action = "GetProducts" } 

Then you need to create the GetProducts action on the Sellers controller, which this route should direct to callers:

 [HttpGet] public IEnumerable<Product> GetProducts(int sellerId) { //TODO: Add query to return results. } 

The sellerId parameter will be passed through the route and should be used to manage your request.

The main drawback of this approach is that the user route makes it difficult to create URLs for links between resources, and when they become more complex, they may begin to map URLs that you did not expect from them.

+3
source

I am looking for a solution to the same problem. I am currently testing this solution.

First of all, change the route:

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

Then, if you want SellersController , write it as usual, but name the entire action "Index" and use the notation to correctly route the http verb. For instance:

  [HttpGet] public List<Seller> Index() {} 

Then, in SellersController create actions, naming them as your "internal" object ( Products ).

  [HttpGet] public List<Product> Products(int id) {} [HttpGet] public Product Products(int id, int id2) {} 

So you can call

  GET /api/sellers or GET /api/sellers/99/products or GET /api/sellers/99/products/2 

I am really testing this approach, so any feedback would be appreciated!

+2
source

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


All Articles