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?
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)
{
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With