2

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.");
    } 
tereško
  • 58,060
  • 25
  • 98
  • 150
SuryaKavitha
  • 493
  • 5
  • 16
  • 36

4 Answers4

4

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.!!!

RL89
  • 1,866
  • 5
  • 22
  • 39
  • RedirectToAction can access in controller only. I am not able to access in my Model class file. Do we need to add any namespace for access this RedirectToAction method? – SuryaKavitha Sep 12 '12 at 12:14
  • You can not access RedirecToAction mehod in Model Class. – RL89 Sep 12 '12 at 12:18
  • So can u please tell me How can i redirect from model to login page? – SuryaKavitha Sep 12 '12 at 12:21
  • you are not clear with the Concept of MVC. Please have a look here http://www.asp.net/mvc/overview/what-is-mvc – RL89 Sep 12 '12 at 12:25
3

Use

Redirect

or RedirectToAction

or RedirectToRoute

Also refer old post ASP.Net MVC Redirect To A Different View

Community
  • 1
  • 1
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
2

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.

Display Name
  • 4,672
  • 1
  • 33
  • 43
1

You can use RedirectToAction()

return RedirectToAction("ActionName", "ControllerName");
jsmith
  • 976
  • 8
  • 19