在我的网站上,我有这样的网址:
https://www.mywebsite.com/View/Index/{MongoDbId}控制器返回带有产品详细信息的视图。
我的产品类DTO (只是重要的领域)
public class ProductDto
{
public string Id {get; set;}
public string Name {get; set;}
}此时,我有一个名为View的控制器和一个处理请求的Index方法,但是我希望有这样的内容:
https://www.mywebsite.com/v/56b8b8801561e80c245a165c/amazing-product-name实现这一点的最佳方法是什么?
我在ASP.NET Core (GitHub官方项目)中读过关于路由的文章,但我不太清楚该如何做。
谢谢!!
发布于 2016-03-11 15:35:18
若要在ASP.NET核心中全局配置路由,请在Startup.cs中使用扩展方法:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});在您的例子中,对于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"});
});然后,您的操作应该处理customId和customName参数如下:
public IActionResult Index(string customId, string customName)
{
//customId will be 56b8b8801561e80c245a165c
//customName will be amazing-product-name
}有关ASP.NET核心中路由的更多信息,请转到:http://docs.asp.net/en/latest/fundamentals/routing.html?highlight=routing
发布于 2016-03-11 15:20:51
您可以使用路由在.NET框架中创建虚荣心URL。属性-路由-asp-net-mvc-5
基本的概述是,您可以这样装饰您的行为:
[Route("Album/Edit/{id:int}")] public ActionResult Edit(int id)
https://stackoverflow.com/questions/35943643
复制相似问题