无法将 url 作为重定向参数传递

Cannot pass url as a redirect parameter

我想重定向到以下操作方法:

public ActionResult Display(string myUrl)
{
    ViewBag.MyUrl = myUrl;
    return View();
}

如果我尝试以下代码:

public ActionResult AnotherActionInTheSameController(string myUrl)
{
    // ... 
    return RedirectToAction("Display", new { myUrl = "domain.com" });
}

它将生成以下重定向 link:

`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 问题的答案(在路线中添加 *)但没有成功。

我在这里看到的问题是您想将 url 值作为查询字符串 myUrl 传递。您不需要在路由中将查询字符串作为 url 的一部分。

请从以下路线中删除 {myUrl}。然后一切都会好起来的。

routes.MapRoute(
name: "test",
url: "{controller}/Display",
        defaults: new { controller = "MyController", action = "Display" }
 );