2009-09-02 58 views
2

当我的网站上的任何URL是404时,我想显示一个使用ASP.NET-MVC呈现的自定义404页面。 Hoewever我不想使用通配符路由方法,因为这会禁用标准webforms。我的代码目前看起来是这样的:如何在ASP.NET-MVC中显示自定义404页面?

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

这个代码是内部App_Error

// 1不工作。 Test.aspx的是一个标准的Web窗体

,因为它是一个asp.net MVC的路线

如何使MVC路线的工作

// 2不起作用?

回答

-1

您可以使用应用程序错误事件......是这样的:

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)); 
    } 
} 
+1

我不想在任何情况下重定向。我想要显示MVC-View。 – usr 2009-09-02 15:08:24

+0

是的,只要你有一个ErrorController和适当的方法,就会显示一个MVC-View。 – Robban 2009-09-02 15:37:48

+0

它会,但通过重定向... – usr 2009-09-02 17:25:38