Reputation: 12715
I have a controller, and to invoke all its actions the user has to have privilages to do that. The question is how to check that before action is executed? If the user doesn't have permissions I want to render a View with error message. I tried to use overriden OnActionExecuting
method, but I can't return a View from that method
Upvotes: 1
Views: 1272
Reputation: 1039548
I tried to use overriden OnActionExecuting method, but I can't return a View from that method
As a matter of fact you can:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
bool userHasPermissions = CheckUserPermissionsFromSomewhere(filterContext);
if (!userHasPermissions)
{
filterContext.Result = new ViewResult
{
// you can also specify master page and view model
ViewName = "Forbidden"
};
}
else
{
base.OnActionExecuting(filterContext);
}
}
Upvotes: 3