Reputation: 12726
I know how to do redirect from parent controller, suppose I have
public class _ParentController : Controller {
...
}
public class HomeController : _ParentController {
...
}
I can add a method to _ParentController
:
protected override void OnActionExecuting(ActionExecutingContext filterContext) {
if (condition) {
filterContext.Result = Redirect(path);
}
}
Now I need to return a view or do Server.Transfer (I need to preserve the url). Server.TransferRequest
doesn't work for me in this case, is there any other way to do what I need? I use .NET MVC3 and IIS7.5
Thanks.
Upvotes: 2
Views: 896
Reputation: 5666
For example you can have:
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
if (((filterContext.Exception is SecurityException)) ||
((filterContext.Exception is AuthenticationException)))
{
filterContext.ExceptionHandled = true;
filterContext.Result = View("Error", "You don't have permission");
}
}
This will set the result of action to view of your choice, preserving current url. (keep in mind that view have to be found in folder according to current route or in shared folder)
filterContext.Result = View("Name of view", "object model")';
Upvotes: 2