che*_*xis 3 c# asp.net-core-mvc asp.net-core
在我的网站上,我有这种网址:
https://www.mywebsite.com/View/Index/{MongoDbId}
Run Code Online (Sandbox Code Playgroud)
Controller返回包含产品详细信息的View.
我的产品类DTO(只是重要的领域)
public class ProductDto
{
public string Id {get; set;}
public string Name {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
在这一刻,我有一个名为View的控制器和一个处理请求的Index方法,但我希望有这样的东西:
https://www.mywebsite.com/v/56b8b8801561e80c245a165c/amazing-product-name
Run Code Online (Sandbox Code Playgroud)
实现这个的最佳方法是什么?
我已经阅读过关于ASP.NET Core (GitHub上的官方项目)的路由,但我不太清楚如何做.
谢谢!!
要在ASP.NET Core中全局配置路由,请在Startup.cs中使用扩展方法:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)
对于你的情况,对于url:
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"});
});
Run Code Online (Sandbox Code Playgroud)
然后您的操作应该处理customId和customName参数,如下所示:
public IActionResult Index(string customId, string customName)
{
//customId will be 56b8b8801561e80c245a165c
//customName will be amazing-product-name
}
Run Code Online (Sandbox Code Playgroud)
有关在ASP.NET Core中路由的更多信息,请访问:http: //docs.asp.net/en/latest/fundamentals/routing.html?highlight = DOC
| 归档时间: |
|
| 查看次数: |
2749 次 |
| 最近记录: |