只是路线定义操作'bookshop/{pageName}'
以下是使用路由属性2种方案的示例:
在情况下,你不想要的网址更改:
[Route("bookshop/{pageName}")]
public ActionResult MyAction(string pageName)
{
// add logic according to what you receive in pageName property
return View();
}
或者,如果你想重定向到一个新的URL:
[Route("bookshop/{pageName}")]
public ActionResult MyAction(string pageName)
{
// Create and use a method to ExtractProductNameFromPageName
string productName = ExtractProductNameFromPageName(pageName);
return Response.Redirect("~/" + productName);
}
参数'pageName'
这里应该抓取页面名称过去'bookshop/'
。
在情况下,你没有启用路由属性映射,RegisterRoutes
方法RouteConfig.cs
文件添加以下代码:
// enable mapping of routes defined using Route attribute on specific actions.
routes.MapMvcAttributeRoutes();
感谢@sachin的答案 – bhargav