【问题标题】:Return Response.Redirect or ActionResult from a single MVC method从单个 MVC 方法返回 Response.Redirect 或 ActionResult
【发布时间】:2018-11-13 23:45:31
【问题描述】:

我有一个当前返回 ActionResultMVC 方法 - 我必须进行一些更改,并根据业务逻辑我想做一个 response.redirect .

所以我想做这样的事情:

public ActionResult Index(CountryHomePageType currentPage)
{
     if (someVar = true)
     {              
         return View();
     }
     else
     {
        Response.redirect("www.website.com")
     }
}

但我不能因为 Resonse.Redirect 不是 ActionResult....

我怎样才能解决这个问题?

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 asp.net-mvc-5


【解决方案1】:

如果您在当前 mvc 应用程序之外进行重定向,您可以使用

return Redirect("<your external url>"); // like "https://www.google.com"

如果你想重定向回你的主页,你可以使用

return RedirectToAction("Index", "Home");

假设您使用的是默认的 mvc 设置

如果您在多个地方进行此检查,您可能还想查看ActionFilters

【讨论】:

  • 编译器无法识别“重定向”,但它确实识别了 RedirectResult(url) - 我猜这是 MVC 版本问题?
  • @AyoAdesina 你用的是什么版本?你用 3,4,5 标记它。我发布的代码是使用 mvc5 并且 Redirect 存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 1970-01-01
相关资源
最近更新 更多