views:

102

answers:

2

Hi,

I have AdminError.aspx and Error.aspx pages and I want to display AdminError.aspx when there is an exception. I need both files.

Why this doesn't work?

<customErrors mode="On" redirectMode="ResponseRedirect" defaultRedirect="AdminError.aspx" />

Instead Error.aspx is always displayed.

What can I do?

+1  A: 

Asp.net mvc provide [HandleError] attribute to handle this kind of requirement, you can specify different error page (view) you want to redirect to based on specific error type. It's very flexible and recommended to do that way.

For example

[HandleError(ExceptionType = typeof(NullReferenceException),
     View = "NullError")]
[HandleError(ExceptionType = typeof(SecurityException),
     View = "SecurityError")]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        throw new NullReferenceException();
    }

    public ActionResult About()
    {
        return View();
    }
}

Check out this similar question to find out more information.

J.W.
+1  A: 

Thanks,

I think I found a solution.

I'm using HandleErrorWithELMAHAttribute (http://stackoverflow.com/questions/766610/) and in OnException method I've set my view:

public override void OnException(ExceptionContext context)
{
   View = "AdminError"; // this is my view
   base.OnException(context);

   var e = context.Exception;
   if (!context.ExceptionHandled   // if unhandled, will be logged anyhow
       || RaiseErrorSignal(e)      // prefer signaling, if possible
       || IsFiltered(context))     // filtered?
   return;

   LogException(e);
}

I've noticed that it works with and without redirectMode and defaultRedirect attributes from customErrors tag.

denis_n