C# 需要重定向到asp.net MVC3中的其他页面

C# 需要重定向到asp.net MVC3中的其他页面,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我正在使用ASP.net和MVC3。我需要显示一个弹出窗口,或者在会话过期时转到登录页面。有谁能告诉我如何重定向到登录页面,该页面的操作名为“index”,控制器名为“Home” 我的代码是: 这个方法在我的模型中。在这个模型中,我必须重定向到登录页面 protected Product GetCurrentCorp(HttpContextBase context) { if (context.Session != null) {

我正在使用ASP.net和MVC3。我需要显示一个弹出窗口,或者在会话过期时转到登录页面。有谁能告诉我如何重定向到登录页面,该页面的操作名为“index”,控制器名为“Home”

我的代码是:

这个方法在我的模型中。在这个模型中,我必须重定向到登录页面

protected Product GetCurrentCorp(HttpContextBase context)
    {

        if (context.Session != null)
        {
            var selectedProduct = context.Session["SelectedProduct"];
            if (selectedProduct != null)
            {
                var product= selectedProduct as Product;
                return product;                   
            }
        }
        // Here I need to redirect to index page(login page)
        throw new ArgumentException("Product is not set, Cannot Continue.");
    } 

如果LoggedOn是您的操作,Account是您的控制器,那么您可以将代码编写为:

return RedirectToAction("LoggedOn", "Account");
希望这对你有帮助

您可以使用

使用


另请参阅旧帖子

这是一个自包含的操作,它向您展示了重定向到不同操作或URL的示例

public ActionResult MyAction()
{
  // Use this for action, can also be used to redirect 
  // to action on different controller by adding parameter
  return RedirectToAction("MyActionName");

  // Use this for URL
  return Redirect("http://example.com/foo/bar");
}

希望这对您有所帮助。

为什么要访问模型内部的HttpContext和会话?您不需要从模型重定向,需要在控制器中的操作方法中执行此操作。@nan:在这个模型中,我只检查用户凭据的授权。所以我只需要在这里重定向。基本上你的控制器负责重定向。模型不应该这样做。那么我们如何在模型中访问它呢?你能给我一些建议吗?简单地从控制器中调用模型并在控制器中检查返回的产品。若从模型返回的产品在控制器中为空,则使用RedirectToActionRedirectToAction只能在控制器中访问。我无法访问模型类文件中的。我们需要添加任何名称空间来访问这个RedirectToAction方法吗?你不能在模型类中访问RedirecToAction方法。那么你能告诉我如何从模型重定向到登录页面吗?你不清楚MVC的概念。请查看模型中的此处,我们无法访问RedirectToAction()。还有别的办法吗?
public ActionResult MyAction()
{
  // Use this for action, can also be used to redirect 
  // to action on different controller by adding parameter
  return RedirectToAction("MyActionName");

  // Use this for URL
  return Redirect("http://example.com/foo/bar");
}