Blog

Filter posts by Category Or Tag of the Blog section!

Redirect to an action from baseController in MVC

Sunday, 08 February 2015

In Asp.net MVC, in order to redirect to an action you can call RedirectToAction(actionName, ControllerName) easily. But if you would have to do it in BaseController, it doesn't work! For example, if you want to check the nullability of the user session you should initialize the Result of filterContext like this:

protected override void OnActionExecuting(ActionExecutingContext filterContext)

{

if (CurrentUser == null)

        filterContext.Result = new RedirectResult(Url.Action("Login", "Account"));

base.OnActionExecuting();

}

Or maybe in OnException method on MVC:

     protected override void OnException(ExceptionContext filterContext)

        {

               filterContext.HttpContext.Response.Clear();

               filterContext.Result = new RedirectResult(Url.Action("Login", "Account"));

               requestContext.HttpContext.Response.End();

               base.OnException(filterContext);

        }

Cheers!

Category: Software

Tags: Asp.Net

comments powered by Disqus