2009-09-02 78 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