2

So, I'm trying to redirect to an error page in my own AuthorizeAttribute using that:

filterContext.Result = new HttpStatusCodeResult(403);

In my web.config, initially i tried:

<customErrors defaultRedirect="/Error" mode="On">   
    <error statusCode="403" redirect="/UnAuthorize" /> 
</customErrors> 

After i tried:

<customErrors defaultRedirect="/Shared/Error" mode="On">   
    <error statusCode="403" redirect="/Shared/UnAuthorize" /> 
</customErrors> 

And after, i create a ErrorController and i tried that:

<customErrors defaultRedirect="/Shared/Error" mode="On">   
    <error statusCode="403" redirect="/Error/UnAuthorize" /> 
</customErrors> 

But the browser still shows me default error page for 403, any idea?

POSSIBLE SOLUTION: Well, i saw the answers of @bobek and @Robert Levy, but i found another way, a little simpler.

In my AuthorizeAttribute i create a propery called RedirectOnErrorTo, and in OnAuthorization method of my AuthorizeAttribute i did:

if (!string.IsNullOrEmpty(this.RedirectOnErrorTo))
{
    filterContext.Result = new RedirectResult(this.RedirectOnErrorTo);
}

So, now when i declare this attribute i choose to what path i want to redirect.

It's not automated like i wanted using only web.config, but becomes useful. What do you think guys, it's a good way to solve this problem?

1

1 Answer 1

6

Take a look at http://devstuffs.wordpress.com/2010/12/12/how-to-use-customerrors-in-asp-net-mvc-2/

Sign up to request clarification or add additional context in comments.

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.