有没有办法在mvc中更改给定操作的URL而不更改名为的操作或控制器?
如果是这样,将如何在以下MapRoute上完成:
routes.MapRoute(
"Estate.CloseDeal",
"Estate/CloseDeal/{groupId}/{paymentType}/{mortgageValue}/{province}",
new { controller = "Estate", action = "CloseDeal" },
new { groupId = "\\d+", paymentType = "\\d+", mortgageValue = "\\d+", province = "\\d+" }
);
所需的网址是:“... / estate-support / deal-closing / ...”。目前它显示为“... / Estate / CloseDeal /..."
链接到此操作的按钮如下所示:
<button detail="@Url.Action("CloseDeal", new { groupId = info.GroupId })" class="orange">
编辑1:
尝试改为:
routes.MapRoute(
"Estate.CloseDeal",
"estate-support/deal-closing/{groupId}/{paymentType}/{mortgageValue}/{province}",
new { controller = "Estate", action = "CloseDeal" },
new { groupId = "\\d+", paymentType = "\\d+", mortgageValue = "\\d+", province = "\\d+" }
);
此返回错误:您要查找的资源已被删除,名称已更改或暂时不可用。
编辑2:
更改第二个字符串适用于所有路由但是这个 - 不同之处在于,此路由具有其他参数(groupID,paymentType等)。
答案 0 :(得分:3)
只需将第二个字符串中的"Estate/CloseDeal"
替换为"estate-support/deal-closing"
- 应该可以正常工作。
在这种特殊情况下,这很容易,因为路径没有通过控制器和动作名称进行参数化 - 即路线中没有"{controller}/{action}"
。
答案 1 :(得分:0)
您需要更新Url.Action
个调用的参数,或者让您想要呈现的路由与Url.Action调用的参数匹配(即,如果使用Url&gt;名称应匹配;使用名称的操作覆盖)
请注意,如果您希望人们可以将旧网址添加到收藏夹中,您可能希望将旧网址映射到新网址并添加路由,该网址只会重定向到新网址。
答案 2 :(得分:0)
您还可以使用RouteAttribute将路由直接应用于操作(如下所示)或控制器类本身
// Your controller class
// (You could add a Route attribute here)]
public class Estate
{
// Directly apply a route (or 2?!) to this action
[Route("estate-support/deal-closing")]
[Route("clinched")] // You can add multiple routes if required
public ActionResult CloseDeal()
{
...
}
// And of course you can parameters to the route too, such as:
[Route("customers/{customerId}/orders/{orderId}")]
public ActionResult GetOrderByCustomer(int customerId, int orderId)
{
...
}
...
}
为此,您需要通过调用RouteConfig.cs
中的MapMvcAttributeRoutes来启用它:
public static void RegisterRoutes(RouteCollection routes)
{
// Enable mapping by attibute in the controller classes
routes.MapMvcAttributeRoutes();
...
}
来自docs.microsoft.com的更多信息:Attribute Routing in ASP.NET
在这里:C# Corner - route attribute in MVC