[英]Friendly URLs in ASP.NET MVC 6 (ASP.NET Core or ASP.NET5)
In my web site I have this kind of URLs: 在我的网站上,我有这种网址:
https://www.mywebsite.com/View/Index/{MongoDbId}
The Controller return a View with the product details. Controller返回包含产品详细信息的View。
My product class DTO (just important fields) 我的产品类DTO(只是重要的领域)
public class ProductDto
{
public string Id {get; set;}
public string Name {get; set;}
}
In this moment I have a controller called View, and an Index method that process the request, but I would like to have something like this: 在这一刻,我有一个名为View的控制器和一个处理请求的Index方法,但我希望有这样的东西:
https://www.mywebsite.com/v/56b8b8801561e80c245a165c/amazing-product-name
What is the best way to implement this? 实现这个的最佳方法是什么?
I have read about routing in ASP.NET Core (official project on GitHub) , but I don't have very clear how to do. 我已经阅读过关于ASP.NET Core (GitHub上的官方项目)的路由,但我不太清楚如何做。
Thanks!! 谢谢!!
To globally configure routing in ASP.NET Core, use extension method in Startup.cs: 要在ASP.NET Core中全局配置路由,请在Startup.cs中使用扩展方法:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
In your case, for url: https://www.mywebsite.com/v/56b8b8801561e80c245a165c/amazing-product-name
it could look like this: 在您的情况下,对于网址:
https://www.mywebsite.com/v/56b8b8801561e80c245a165c/amazing-product-name
: https://www.mywebsite.com/v/56b8b8801561e80c245a165c/amazing-product-name
它可能如下所示:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "v/{customId}/{customName}",
defaults : new{controller = "View", action = "Index"});
});
And then your action should handle customId and customName parameters as follows: 然后您的操作应该处理customId和customName参数,如下所示:
public IActionResult Index(string customId, string customName)
{
//customId will be 56b8b8801561e80c245a165c
//customName will be amazing-product-name
}
For more info about routing in ASP.NET Core go to: http://docs.asp.net/en/latest/fundamentals/routing.html?highlight=routing 有关在ASP.NET Core中路由的更多信息,请访问: http : //docs.asp.net/en/latest/fundamentals/routing.html?highlight = DOC
You can create Vanity URLs in the .NET framework using Routing. 您可以使用Routing在.NET框架中创建虚荣URL。 attribute-routing-in-asp-net-mvc-5
属性路由-在-ASP净MVC -5-
The basic overview is you can decorate your actions like this: 基本概述是您可以像这样装饰您的操作:
[Route("Album/Edit/{id:int}")] public ActionResult Edit(int id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.