How to achieve seo friendly collection/item urls with .net core mvc routing

Karen Comber 20 Reputation points
2024-03-13T21:02:03.1166667+00:00

Hello,

I would much appreciate some advice on page routing in .net core MVC.

I would like my page links to have seo friendly text urls rather than ids. My site comprises of categories (painting subject collections) and items (paintings). Each collection and painting has a unique url in its corresponding table on the database.

My previous website was built with .net forms. It had the same structure and I was able to have the urls display as text by using web.config with url rewrites to display the correct collection/painting from the dynamic pages behind them.

I have been learning .net core mvc for my new website and I have the website working in that I can display the correct category page and product page but the urls are as follows:

https://localhost:44304/Collections/Index/4

https://localhost:44304/Paintings/Index/11

I would like them to display as follows (or similar but to show urls rather than ids):

https://localhost:44304/Collections/collection-name

https://localhost:44304/Paintings/painting-name

I have done alot of googling but the .net support page for routing is very long and appears quite complex with different ways and I'm unclear as to which one is the resolution I am looking for. Other research hasn't helped either.

My code snippets are as follows:

Startup.cs:

		`app.UseEndpoints(endpoints =>`

{

endpoints.MapControllerRoute(

name: "default",

pattern: "{controller=Home}/{action=Index}/{id?}");

});

I know the pattern needs changing but whatever I try doesn't work.

Code that runs the link to a collection:

				`<a class="item" asp-area="" asp-controller="Collections" asp-action="Index" asp-route-id="@item.Collection.Id">`

...

</a>

CollectionsController.cs

public IActionResult Index(int? id)

{

...

return View(listingsPage);

}

This then runs the view page to display paintings within the collection id passed through.

So the above works fine with the id but with the result of the id in the url. I have tried making changes throughout replacing the id with the url but I am just guessing. I know I may need to spend quite a bit more time to really understand how routing works but if there is a quick solution to this or useful pointers I would very much appreciate it.

Thank you.

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 78,161 Reputation points Volunteer Moderator
    2024-03-14T01:26:31.67+00:00

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.