0

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.

Community
  • 1
  • 1
Bilal Ahmed
  • 235
  • 2
  • 6
  • 17

2 Answers2

0

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

congiuluc
  • 1
  • 2
0

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.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
  • I have tried this approach. I redirects to login page correctly if I Ctrl + F5 but if I click on a link in page, I receive page saying "undefined", I guess "Undefined" text is coming because of jquery mobile. – Bilal Ahmed Feb 05 '15 at 08:56
  • It's possible that "undefined" is coming from your JavaScript e.g. jQuery Mobile. Open up the debugging tools in the browser and see what JavaScript errors are being logged. That may give you a clue as to what is going on. – Jason Evans Feb 05 '15 at 08:58