Filter posts by Category Or Tag of the Blog section!

Redirect to an action from baseController in MVC

Sunday, 08 February 2015

In 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"));



Or maybe in OnException method on MVC:

     protected override void OnException(ExceptionContext filterContext)



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





Category: Software

Tags: Asp.Net

comments powered by Disqus