【发布时间】:2020-01-01 21:51:16
【问题描述】:
我想重定向到以下操作方法:
public ActionResult Display(string myUrl)
{
ViewBag.MyUrl = myUrl;
return View();
}
如果我尝试以下代码:
public ActionResult AnotherActionInTheSameController(string myUrl)
{
// ...
return RedirectToAction("Display", new { myUrl = "domain.com" });
}
它将生成以下重定向链接:
`hostName/ControllerName/Display/domain.com`
但是domain.com参数没有传递给myUrl参数...
如果我尝试以下 url,那么它将达到所需的操作:
`hostName/ControllerName/Display/?myUrl=domain.com`
这是我的路线:
routes.MapRoute(
name: "test",
url: "{controller}/Display/{myUrl}",
defaults: new { controller = "MyController", action = "Display", myUrl = UrlParameter.Optional }
);
我已经尝试了this 问题中的答案(在路线中添加 *),但没有奏效。
【问题讨论】:
-
您的路线错误。从路由中删除 myUrl 它将正常工作。
-
很好。我会将我的评论移至答案。请接受。
标签: asp.net-mvc redirect routing