4

In my web site I have this kind of URLs:

https://www.mywebsite.com/View/Index/{MongoDbId}

The Controller return a View with the product details.

My product class DTO (just important fields)

public class ProductDto
{
   public string Id {get; set;}
   public string Name {get; set;}
}

In this moment I have a controller called View, and an Index method that process the request, but I would like to have something like this:

https://www.mywebsite.com/v/56b8b8801561e80c245a165c/amazing-product-name

What is the best way to implement this?

I have read about routing in ASP.NET Core (official project on GitHub), but I don't have very clear how to do.

Thanks!!

Marcin Zablocki
  • 10,171
  • 1
  • 37
  • 47
chemitaxis
  • 13,889
  • 17
  • 74
  • 125
  • If you want your URLs to be 100% SEO friendly (with no ID in the URL), the best way is to use [data-driven routing by implementing `IRouter`](http://stackoverflow.com/questions/32565768/change-route-collection-of-mvc6-after-startup#32586837). – NightOwl888 Mar 11 '16 at 16:23
  • But I have about 80000 items, and more... Can I use it without performance problems? – chemitaxis Mar 11 '16 at 16:56
  • And in this moment... I dont have URL field... – chemitaxis Mar 11 '16 at 16:57
  • 1
    I am just saying there are other options than the accepted answer. You will definitely get better performance with that solution, but your site may not rank as well in the SERPs as a site without an ID in the URL. There is no one-size fits all solution for everyone, you need to decide based on whether performance or SEO is more important. Of course, when using caching (as shown in the answer) performance will depend on how much memory you have available and how well you have optimized your route instances into different caches based on URL popularity. – NightOwl888 Mar 11 '16 at 17:08

2 Answers2

8

To globally configure routing in ASP.NET Core, use extension method in Startup.cs:

 app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

In your case, for url: https://www.mywebsite.com/v/56b8b8801561e80c245a165c/amazing-product-name it could look like this:

 app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "v/{customId}/{customName}",
                    defaults : new{controller = "View", action = "Index"});
            });

And then your action should handle customId and customName parameters as follows:

public IActionResult Index(string customId, string customName)
{
   //customId will be 56b8b8801561e80c245a165c
   //customName will be amazing-product-name
}

For more info about routing in ASP.NET Core go to: http://docs.asp.net/en/latest/fundamentals/routing.html?highlight=routing

Marcin Zablocki
  • 10,171
  • 1
  • 37
  • 47
  • Thanks @Marcin ;) Good answer ;) Can you please update in this case: https://www.mywebsite.com/v/56b8b8801561e80c245a165c/amazing-product-name (the different is what the name is not with "-", is with "/" – chemitaxis Mar 11 '16 at 15:36
  • Wow!! So fast!! ;) Thanks!! – chemitaxis Mar 11 '16 at 15:36
0

You can create Vanity URLs in the .NET framework using Routing. attribute-routing-in-asp-net-mvc-5

The basic overview is you can decorate your actions like this:

[Route("Album/Edit/{id:int}")] public ActionResult Edit(int id)

Jared Stroebele
  • 574
  • 6
  • 25