我在 .NET Core 3.1 中有一个应用程序,我应该重写搜索引擎优化的 URL。
目前 url 是标准的 MVC 模式(/Controller/Action/Id),例如:
/新闻/详情/21
endpoints.MapControllerRoute (
name: "default",
pattern: "{controller = Home}/{action = Index}/{id?}");
最佳结果将是:
/en/news/21/title-news
我该如何继续?我已经尝试过,但在我看来,从重写到中间件的技术太多了,我无法理解哪一种适合我。
如果可能,您是否有任何示例可以以简单的方式执行此操作?
最佳答案
在默认路由之前为新闻添加一条新路由:
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?}");
});
Controller Action :
public class HomeController : Controller
{
public ActionResult NewsAction(string lng, int id, string titleNews)
{
...
}
}
关于asp.net-mvc - .Net Core 中的 SEO 网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61572343/