It generally referred to slugs. See docs:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-8.0
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
It generally referred to slugs. See docs:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-8.0