Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SEO Url in .Net Core

I have an application in .NET Core 3.1 and I should rewrite search engine optimized URLs.

At the moment the urls are the standard ones of the MVC pattern (/Controller/ Action/Id) for example: /News/Detail/21

 endpoints.MapControllerRoute (
     name: "default",
     pattern: "{controller = Home}/{action = Index}/{id?}");

The optimal result would be: /en/news/21/title-news

How can I proceed? I have tried but there seem to me too many techniques from rewrite to middleware and I can't understand which one is right for me.

Do you have any examples to do this in a simple way if possible?

like image 677
Diegos Avatar asked Oct 30 '25 11:10

Diegos


1 Answers

Add a new route for news before the default route:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "news",
        pattern: "{lng}/news/{id}/{titleNews}", 
        new { controller = "Home", action = "NewsAction" });

    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

The controller action:

public class HomeController : Controller
{
    public ActionResult NewsAction(string lng, int id, string titleNews)
    {
        ...
    }
}
like image 166
Martin Staufcik Avatar answered Nov 02 '25 18:11

Martin Staufcik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!