代码之家  ›  专栏  ›  技术社区  ›  Ayo Adesina

从单个MVC方法返回response.redirect或actionResult

  •  1
  • Ayo Adesina  · 技术社区  · 6 年前

    我有一个 MVC 当前返回 行动结果 -我不得不做一些更改,并基于业务逻辑,我想做一个response.redirect代替。

    所以我想这样做:

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

    但我不能成为理性。重定向不是一个行动结果…

    我怎样才能避开这件事?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Fran    6 年前

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

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

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

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

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

    你可能还想看看 ActionFilters 如果你是在多个地方做这个检查。