Vinicius Ottoni
Vinicius Ottoni

Reputation: 4677

How can I redirect to an error page in asp.net mvc 3?

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?

Upvotes: 2

Views: 19006

Answers (1)

Related Questions