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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Vinicius Ottoni
  • 4,631
  • 9
  • 42
  • 64
  • Check out my answer here http://stackoverflow.com/questions/8732048/mvc3-custom-error-pages-work-in-dev-not-on-server/8734547#8734547 – bobek Jan 20 '12 at 17:59

1 Answers1