ASP.NET MVC routing: with a request for one action, without another

I need the following routes:

example.com/products
Goes to the product category page (e.g. cars, trucks, buses, bicycles)
controller = Products , action = Categories()

example.com/products?a=1&b=2
refers to the index of all products in a certain category (e.g. Ford, Honda, Chevy)
controller = Products , action = Index(string a, string b)

Routes differ only by request, and it seems that MVC does not ignore anything after "?". So, of course, only one rule will ever come across - the first.

How can I distinguish between two?

Edit: indicated differently, I want two routes. Is it possible to use a request on a route or is MVC really ignoring it? Is there a way to hack it or use some special routing scheme, just as I can do user binding and user check?

+1
source share
1 answer

Enter the parameters. ASP.NET MVC allows you to create "beautiful" URLs and what exactly you should do here:

First, route mapping:

 routes.MapRoute( "SpecificProducts", "products/{a}/{b}", new { controller = "products", action = "Categories" } ); routes.MapRoute( "ProductsIndex", "products". new { controller = "products", action = "Index" } ); 

Then controller actions

 public ActionResult Index() { } public ActionResult Categories(string a, string b) //parameters must match route values { } 

This allows you to use a search-friendly URL and you don’t have to worry about query string options.

+1
source

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


All Articles