Asp.net 切换到{controller}/{id}/{action}会中断重定向到操作
我正在尝试使用适当的Asp.net 切换到{controller}/{id}/{action}会中断重定向到操作,asp.net,asp.net-mvc-3,url-routing,asp.net-mvc-routing,Asp.net,Asp.net Mvc 3,Url Routing,Asp.net Mvc Routing,我正在尝试使用适当的RESTurl和MVC。为此,我从以下位置切换了默认路由: {controller}/{action}/{id} 到 因此,不是: /Customer/Approve/23 现在有了 /Customer/23/Approve ActionLink似乎工作正常,但CustomerController中的以下代码: [CustomAuthorize] [HttpGet] public ActionResult Approve(int id) { _customerS
REST
url和MVC
。为此,我从以下位置切换了默认路由:
{controller}/{action}/{id}
到
因此,不是:
/Customer/Approve/23
现在有了
/Customer/23/Approve
ActionLink似乎工作正常,但CustomerController中的以下代码:
[CustomAuthorize]
[HttpGet]
public ActionResult Approve(int id)
{
_customerService.Approve(id);
return RedirectToAction("Search"); //Goes to bad url
}
在url/Customer/23/Search
上结束。而它应该转到/Customer/Search
。不知怎的,它记得23(id)
这是我在global.cs中的路由代码
routes.MapRoute(
"AdminRoute", // Route name
"{controller}/{id}/{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { id = new IsIntegerConstraint() }
);
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index" });
如果我切换这两个功能,重定向到操作
开始工作,但使用:
Html.ActionLink("Approve", "Approve", new { Id = 23})
现在生成/Customer/Approve?id=23
,而不是/Customer/23/Approve
我可以指定直接URL,比如~/Customer/23/Approve
,而不是使用ActionLink
和redirectoaction
,但我更愿意坚持使用MVC
提供的函数,尝试在控制器中传递新的(空的)RouteValueDictionary
return RedirectToAction("Search", new System.Web.Routing.RouteValueDictionary{});
在这里:
Html.ActionLink("Approve", "Approve", new { Id = 23})
我甚至不知道它如何获取客户控制器,因为您没有在任何地方指定它。尝试向ActionLink helper提供控制器和操作。当您在内部使用RedirectToAction()时,MVC将使用现有路由数据(包括Id值)来构建url。即使传递空RouteValueDictionary,现有路由数据也将与新的空路由值数据合并 我能看到的唯一解决方法是使用RedirectToRoute(),如下所示:
return RedirectToRoute("Default", new { controller = "Customer", action = "Search"});
CouncellorBen尝试在控制器操作中将当前路由数据传递给methon:
return RedirectToAction("Search", this.RouteData.Values);
删除此部分:
id = UrlParameter.Optional
可以解决问题;当您将“id”定义为可选参数,并且拥有“Default”映射时,“Default”和“AdminRoute”一起是相同的!
问候。我也有类似的问题。当我尝试使用重定向到操作重定向用户时,传递给控制器操作的路由值被重新使用,即使我没有在新的路由值字典中指定它们。我(在阅读之后)想出的解决办法 顾问本的职务是清除当前请求的路由数据。这样,我就可以阻止MVC合并我没有指定的路由值 所以,在你的情况下,也许你可以这样做:
[CustomAuthorize]
[HttpGet]
public ActionResult Approve(int id)
{
_customerService.Approve(id);
this.RouteData.Values.Clear(); //clear out current route values
return RedirectToAction("Search"); //Goes to bad url
}
我有一个类似的问题,并且能够通过将id添加到默认路由来解决它
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
如果默认路线中确实没有id,则您也可以尝试:
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index", id = string.Empty });
我想知道id上的UrlParameter.Optional是否对此有影响奇怪的是UrlParameter只有一个值“Optional”,类似“Required”的值可能会使它工作。如果在控制器内调用RedirectToAction(“action”),则ControllerName是可选的。如果在控制器的视图中执行ActionLink,效果似乎相同。它只使用当前控制器。是的,但为了获得额外的确定度,当您遇到无法解释的问题时,您应该指定控制器,您不认为吗?关于“额外的确定度”的观点很好。我尝试添加控制器名称,但没有更改。不幸的是,这仍然将custumer id保留在url中,最终为/Customer/23/Search
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index", id = string.Empty });