1

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

2 Answers 2

3

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It works, thanks! :) I'm also wondering how to create a 'base' method which will be rendering a view with error messages (the message will be used as a parameter). Any ideas ?
0

In the class Controller this method is protected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.