Stop response at ActionFilterAttribute validation

actionfilterattribute, asp.net-mvc-4

Solution

Just set the `Result` of the `filterContext`:

filterContext.Result = new HttpUnauthorizedResult("Invalid Request !!");

If you want to allow access to authenticated users only, you better use the `Authorize` filter instead.

UPDATE:

Since you want to render an error View upon failed authorization, try this:

filterContext.Result = new ViewResult
        {
        ViewName = "{YourErrorViewName}",
        ViewData = { Model = new Exception("Invalid Request !!") }
        }; 

Problem

I have a, ActionFilterAttribute and on its OnExecuting event, I am validating the request. And if it's not an authorized request, I am doing something like: ``` if(notAuthorized){ filterContext.HttpContext.AddError(new Exception("Invalid Request !!")); } ``` So, for the unauthorized requests, it adds the exception and continues executing the action. My question is how to stop the processing at this point? I have tried: ``` filterContext.HttpContext.Response.End(); ``` but no success. Please reply. Checking for errors in the action will be an option though, but I am looking for the change in the action filter itself, so that we don't need to change in each action.

Original source