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!!

4
  • 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. Commented Mar 11, 2016 at 16:23
  • But I have about 80000 items, and more... Can I use it without performance problems? Commented Mar 11, 2016 at 16:56
  • And in this moment... I dont have URL field... Commented Mar 11, 2016 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. Commented Mar 11, 2016 at 17:08

2 Answers 2

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

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

2 Comments

Thanks @Marcin ;) Good answer ;) Can you please update in this case: mywebsite.com/v/56b8b8801561e80c245a165c/amazing-product-name (the different is what the name is not with "-", is with "/"
Wow!! So fast!! ;) Thanks!!
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)

4 Comments

Your example is from WebForms, not valid in this case. And... what happens with the name param?
@chemitaxis: It's for MVC.
The example is loosely from the MVA course Introduction to ASP.NET MVC
How can I register the new router in ASP.NET Core?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.