2

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?

usr
  • 168,620
  • 35
  • 240
  • 369

1 Answers1

-1

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
  • 6,729
  • 2
  • 39
  • 47