Creating dynamic routes in mvc5

Based on my recommendation from the SEO team, I am trying to create friendly SEO URLs. For some static pages, I made it easy using RouteCollection.MapRoute (), like -

//Home/Solutions
routes.MapRoute("Solutions", "Solutions", new { controller = "Home", action = "Solutions" }, new[] { "MyAuction.Controllers" });

//Home/SolutionOfferings
routes.MapRoute("Offerings", "Offerings", new { controller = "Home", action = "SolutionOfferings" }, new[] { "MyAuction.Controllers" });

//Home/Pricing
routes.MapRoute("Pricing", "Pricing", new { controller = "Home", action = "Pricing" }, new[] { "MyAuction.Controllers" });

Then I tried to create SEO friendly routes for my dynamic routes. For example, several auctions are planned during the day that contain hundreds of cars planned at the auction. To show the details of this planned car at auction, the actual URL is somewhat -

http://example.com/Auction/VehicleDetails?AuctionId=42&VehicleId=101 Please note that VehicleIdis an identifier in the table AuctionVehicles, which also contains other information about the vehicle, such as Make, Model, Yearand VINetc.

What I want to achieve is to create a dynamic URL, for example -

http://example.com/42/honda-civic-2010-123456

where 42 is the auction identifier, and honda is make, civic is the model, 2010 is the year, and 123456 is the last 6 digits of the VIN.

I don’t know how to achieve this.

I tried using this link - Dynamic Routes from Database for ASP.NET MVC CMS

Any help would be greatly appreciated.

+4
source share
2 answers

Routing is one of the most difficult things to understand in mvc. The best way I've found is tracing MVC attributes in ASP.NET MVC 5. (Ps I'm typing on the phone)

you just include this line in your RouteConfig

 routes.MapMvcAttributeRoutes();

URL- , :

 [Route("books/{bookName?}")]
 public ActionResult View(string bookName)
 {
      if (!String.IsNullOrEmpty(bookName)
      {
           return View("OneBooks"), GetBooks(bookName));
      }
      return View("AllBooks"), GetBooks());
 }

URL www.example.com/books/jungle-book

, . , :

https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/

, mvc ( !!):

https://www.codeproject.com/Articles/641783/Customizing-Routes-in-ASP-NET-MVC

, , - !

+1

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


All Articles