skip to Main Content

I currently map my controller routes like this:

var webApplication = builder.Build();
webApplication.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");

This works great for my default scenario where I don’t provide action name or id in the parameters. So for example the following routes work:

https://example.com.com
https://example.com.com/Home
https://example.com.com/Books
https://example.com.com/Books/Show/1

However, I am moving to SEO more friendly names for some of the products and instead of /1 I want to have /my-book-name. However, I still want the old way to behave the same. I just want to be able to type now:

https://example.com.com/Books/Show/my-book-name

I do not wish to have to do this on every action method individually, so I tried the following:

var webApplication = builder.Build();
webApplication.MapControllerRoute("default-uri", "{controller}/Show/{uri}");
webApplication.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");

However, the above does not work when I enter

https://example.com.com/Books/Show/my-book-name

How can I achieve the above? I am using .NET Core 8

2

Answers


  1. In the route configuration that you created for the scenario of ‘/my-book-name’, you can add a third parameter of ‘constraints’ and specify a regular expression that allows any word with dashes for ‘uri’ route parameter.

    So the updated route configuration will look something like this-

    webApplication.MapControllerRoute("default-uri", "{controller}/Show/{uri}", constraints: new { uri = @"[w-]+" });
    
    Login or Signup to reply.
  2. Multiple conventional routes can be configured by adding more calls to MapControllerRoute and MapAreaControllerRoute. Doing so allows defining multiple conventions, or to adding conventional routes that are dedicated to a specific action, such as:
    
    app.MapControllerRoute(name: "blog",
                    pattern: "blog/{*article}",
                    defaults: new { controller = "Blog", action = "Article" });
    app.MapControllerRoute(name: "default",
                   pattern: "{controller=Home}/{action=Index}/{id?}");
    
    
    The blog route in the preceding code is a dedicated conventional route. It's called a dedicated conventional route because:
    
    It uses conventional routing.
    It's dedicated to a specific action.
    Because controller and action don't appear in the route template "blog/{*article}" as parameters:
    
    They can only have the default values { controller = "Blog", action = "Article" }.
    This route always maps to the action BlogController.Article.
    /Blog, /Blog/Article, and /Blog/{any-string} are the only URL paths that match the blog route.
    
    The preceding example:
    
    blog route has a higher priority for matches than the default route because it is added first.
    Is an example of Slug style routing where it's typical to have an article name as part of the URL.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search