【问题标题】:ASP.NET MVC RedirectToAction Default ActionASP.NET MVC RedirectToAction 默认操作
【发布时间】:2016-07-03 19:21:49
【问题描述】:

如何在不定义动作的情况下重定向到控制器? (它将默认为路由配置中定义的默认操作)

示例 return RedirectToAction("Index", "Error" new { messageId = errorMessageId} );

显示给用户的网址是:http:mydomain/Error/Index/1001

首选 return RedirectToAction(null, "Error" new { messageId = errorMessageId} );

我要显示给用户的网址是:http:mydomain/Error/1001

当我手动输入 URL 时,它工作正常,所以我知道我的路由正常工作。

我只是不知道如何在 MVC 中使用 Redirectxxxx。

【问题讨论】:

  • 您可以在 MVC 中直接使用Redirect("url goes here"),您可以在其中直接输入您的网址“http:mydomain/Error/1001”
  • When I type the URL manually, it works fine, so I know my routes are working. - 不一定。传入 URL 和传出 URL 是两种不同的方法,很可能以不匹配的方式配置路由。请发布您的路由配置 - 您的示例 URL 可能“有效”,因为它匹配的路由与您预期的不同。

标签: c# asp.net asp.net-mvc asp.net-mvc-4 url-routing


【解决方案1】:

您可以使用RedirectToRoute

return RedirectToRoute(new { controller = "Error", messageId = errorMessageId });

另外,还有Redirect 加上Url.RouteUrl

return Redirect(Url.RouteUrl(new { controller = "Error", messageId = errorMessageId }));

【讨论】:

    【解决方案2】:
      protected internal RedirectToAction(
        string actionName,
        string controllerName,
        object routeValues
    )
    

    这里是 RedirectToAction 方法,您可以在此处定义自己的路线,而无需使用操作名称。

    有关路由的更多信息,请查看: Routing

    【讨论】:

      猜你喜欢
      • 2018-12-08
      • 1970-01-01
      • 2010-09-25
      • 2018-04-29
      • 2021-06-17
      • 2011-02-23
      • 2011-08-01
      • 2019-06-06
      • 2012-08-14
      相关资源
      最近更新 更多