1

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?

2 Answers 2

1

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.

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

Comments

1

Thanks,

I think I found a solution.

I'm using HandleErrorWithELMAHAttribute (How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?) 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.

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.