0

How can I use the title instead of displaying the ID at the end of the URL?

I have developed a website using .NET 6 and Razor Page. In this website, there is a blog section which can be accessed by clicking on it to display a list of articles. By selecting an article, the details of the article can be displayed using the ID address of the article. Consider the following URL: https://localhost:44398/blog-details/4 The number 4 indicates an article with ID 4. According to SEO principles, I would prefer to display the title in the URL instead of the ID address. What can I do to resolve this issue?

Qing Guo
  • 6,041
  • 1
  • 2
  • 10
Ali
  • 5
  • 2
  • do you use two links? https://stackoverflow.com/a/73523734/10193401 or https://stackoverflow.com/questions/2174820/how-to-add-page-title-in-url-in-asp-net-mvc-url-generation – abolfazl sadeghi Jun 20 '23 at 08:58
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jun 20 '23 at 10:54

1 Answers1

0

I would prefer to display the title in the URL instead of the ID address.

Below is a demo, you can refer to it, hope it can help you.

In Index.cshtml, use asp-route-title:

<a asp-page="./Details" asp-route-title="@item.title">Details</a> 

In your Details.cshtml.cs add title parameter:

public async Task<IActionResult> OnGetAsync(string title)
        {...}

result:

enter image description here

Qing Guo
  • 6,041
  • 1
  • 2
  • 10