简体   繁体   中英

Need to redirect to another page in asp.net MVC3

I'm working with ASP.net and MVC3 . I need to show a popup or go to the login page When the session get expired. Can any one please tell me How to redirect to login page which is having action name as "index" and the controller name as "Home".

My Code is:

This method is in my model.In this model I have to redirect to login page.

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.");
    } 

If LoggedOn is your Action and Account is your Controller then you can write your code as:

return RedirectToAction("LoggedOn", "Account");

Hope this will help you.!!!

This is the self contained action that is showing you examples of redirection to different action or 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");
}

Hope this is of help to you.

你可以使用RedirectToAction()

return RedirectToAction("ActionName", "ControllerName");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM