views:

295

answers:

1

When any URL is 404 on my site, i want to show a custom 404 page that is rendered with ASP.NET-MVC. Hoewever i do not want to use the wildcard route approach because that would disable standard webforms. My code currently looks like this:

if (serverException is HttpException && ((HttpException)serverException).GetHttpCode() == 404)
{
 //Server.Transfer("~/Test.aspx"); //1
 //Server.Transfer("~/error/gf54tvmdfguj85fghf/404"); //2
}

this code is inside App_Error

//1 does work. Test.aspx is a standard webform

//2 does not work as it is an asp.net-mvc route

How to make the MVC-route work?

A: 

You could use the application error event... like this:

 protected void Application_Error(object sender, EventArgs e)
{
  //Check if it's a 404 error:
  Exception exception = Server.GetLastError();
  HttpException httpException = exception as HttpException;

  if(httpException.GetHttpCode() == 404) {
      //Redirect to the error route
     Response.Redirect(String.Format("~/Error/404/?message={0}", exception.Message));
  }
}
Robban
I do not want to redirect under any circumstances. I want the MVC-View to be shown.
usr
Yes, this would show an MVC-View as long as you have an ErrorController with the appropriate methods.
Robban
It would, but by redirecting...
usr
Why wouldn't you want to do a redirect?
Manticore
Redirecting on a 404 is a horrible idea. I'm with usr on that one.
chaiguy