Search code examples
asp.net-core-mvcasp.net-mvc-routingasp.net-core-2.0asp.net-routing

Is is possible to make SEO friendly Url's in ASP.NET Core like this one


I wanted to ask you guys if is it possible, to make some routing like this for my project /{action}/{title}?

enter image description here

I was wondering if that is possible, does this url has to be a primary key too? Since there is no ID passed to know which blog post is this.

Thank you.


Solution

  • You can do this quite easily with attribute routing:

    [Route("blogs")]
    public class BlogController
    {
        [AcceptVerbs("GET", "HEAD", Route = "{slug}")]
        public IActionResult View(string slug)
        {
        }
    }
    

    This maps all requests to /blogs/whatever to that action, and sets slug to the value after "/blogs/".