【问题标题】:Cannot pass url as a redirect parameter无法将 url 作为重定向参数传递
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2021-04-07
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多