我想重定向到以下操作方法:
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
参数...
如果我尝试以下网址,则将执行所需的操作:
`hostName/ControllerName/Display/?myUrl=domain.com`
这是我的路线:
routes.MapRoute(
name: "test",
url: "{controller}/Display/{myUrl}",
defaults: new { controller = "MyController", action = "Display", myUrl = UrlParameter.Optional }
);
我已经尝试过在this问题中的答案(向路径添加*,但没有用。
我在这里看到的问题是您希望将url值作为查询字符串myUrl传递。您无需在路由中将查询字符串作为部分url提及。
请从以下路径中删除{myUrl}
。然后,一切都会正常。
routes.MapRoute(
name: "test",
url: "{controller}/Display",
defaults: new { controller = "MyController", action = "Display" }
);