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

问题描述 投票:0回答:1

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

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问题中的答案(向路径添加*,但没有用。

asp.net-mvc redirect routing
1个回答
1
投票

我在这里看到的问题是您希望将url值作为查询字符串myUrl传递。您无需在路由中将查询字符串作为部分url提及。

请从以下路径中删除{myUrl}。然后,一切都会正常。

routes.MapRoute(
name: "test",
url: "{controller}/Display",
        defaults: new { controller = "MyController", action = "Display" }
 );
© www.soinside.com 2019 - 2024. All rights reserved.