4

Based on my SEO team's recommendation i am trying to generate SEO friendly urls. For some static pages i have done that easily 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" });

I was then trying to generate SEO friendly routes for my dynamic routes. For example there are several auctions scheduled for a day which contains hundreds of vehicles scheduled within the auction. To show details of that scheduled vehicle within the auction the actual URL is somewhat -

http://example.com/Auction/VehicleDetails?AuctionId=42&VehicleId=101 Please note that VehicleId represents the Identity within AuctionVehicles table which also contains other details of the vehicle like Make, Model, Year and VIN etc.

What i want to achieve is to generate a dynamic URL like -

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

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

Not sure how to achieve this.

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

Any help would be greatly appreciated.

2
  • Your SEO team needs a refresher course. The idea of "SEO-friendly" routes came about because websites were loading everything under one single document, with URLs like index.asp?page=foo and index.asp?page=bar. This does not apply at all to your scenario here. Query strings are perfectly OK, and a route like you have is already SEO-friendly. Commented Mar 6, 2017 at 14:57
  • Hi can you give me a tick if my answer was correct. Commented Jun 12, 2017 at 10:29

1 Answer 1

1

Routing is one of the most difficult things to grasp in mvc. The best way i have found is MVC attribute routings in ASP.NET MVC 5. (P.s. i'm typing on a phone)

you simply include this line in your RouteConfig

 routes.MapMvcAttributeRoutes();

And then you can set optional parameters and default values and map urls in your actual controllers like this:

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

Your url will look like www.example.com/books/jungle-book

there are many more things you can do. Please read the following article:

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

I also found this links and the sublinks on this page to be helpfull to get a proper understanding of mvc routing (lots of reading!!):

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

As I said I think attribute routing is your best bet!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Harry. Let me review the links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.