0
votes

I am developing Asp.Net MVC 5 jQuery Mobile application. I am facing weird error. When the session is expired and user clicks on any link. A new white page comes with text "undefined". How can I redirect user to Login page.

I tried this, but did not work.

2
Define "didn't work". Please show us some code.xlecoustillier
I gave you the link. I am using accepting answer as solution, but it still shows undefined page instead of taking us to login page. When I press ctrl F5, then it takes me to login page.Bilal Ahmed

2 Answers

0
votes

If you put the [Authorize] Atttribute on your Action it should go directly to your Login page, otherwise check the html link rendered on the client. Bye

0
votes

You could create an action filter for this scenario.

public class SessionExpiredRedirect : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.Controller.ControllerContext.HttpContext.Session == null) // Or however you want to check for expired session.
        {
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary
                {
                    { "Controller", "YourController" },
                    { "Action", "YourAction" }
                });
        }
    }
}

To use the filter you can either place the attribute specifically on the action method you want to redirect if the session has expired:

public class HomeController : Controller
{
    [SessionExpiredRedirect]
    public ActionResult Index()
    {
    }
}

Or in the FilterConfig.cs file:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new SessionExpiredRedirect());
    }
}

In which case the attribute will be executed for all action methods.