2014-09-30 93 views
0

我在我的視圖中設置了/shared/error.cshtml頁面以顯示更友好的錯誤消息。但是當用戶遇到錯誤時它不會顯示。未顯示MVC自定義錯誤

我的web.config文件有:

<system.web> 
<customErrors mode="On" /> 

和我的global.asax.cs有:

public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     filters.Add(new HandleErrorAttribute()); 
    } 

但錯誤消息我得到的仍然是基本的

`Server Error in '/' Application. 

Runtime Error 

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed. 

Details: To enable the details of this specific error message to be viewable on the local server machine, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".` 

我錯過了什麼?

+0

你究竟想達到什麼目的?如果您不指定自定義錯誤頁面,將CustomErrors設置爲On沒有意義?基本上它說,發生錯誤,並顯示自定義錯誤頁面的錯誤... – SmartDev 2014-09-30 09:50:56

+0

我希望它去共享佈局中指定的error.cshtml。它在默認情況下在創建MVC應用程序時執行此操作,但是我們從頭開始構建,因此我不知道如何執行 – LiamHT 2014-09-30 10:03:32

+0

您應該有一個ErrorController,其中包含顯示此視圖的操作。並且該操作應具有以下注釋:[HandleError(View =「Error」,ExceptionType = typeof(Exception))] – SmartDev 2014-09-30 11:03:07

回答

0

激活自定義錯誤在Web.config中加入以下行下

<system.web>: 

<customErrors mode="On" defaultRedirect="/Error"> 
    <error redirect="/Error/NotFound" statusCode="404"/> 
    <error redirect="/Error/InternalServerError" statusCode="500"/> 
</customErrors> 

注意:您可以設置模式=」關」,禁用在開發或調試可能是有幫助的自定義錯誤。設置模式=「RemoteOnly」僅激活遠程客戶端的自定義錯誤,

+0

這將是如果我將我的錯誤指向控制器或任何自定義視圖,我不是。我試圖使用默認功能。此外,即使當我添加了。它不起作用。 在所有應有的尊重,措辭另一個答案SA不幫助我,特別是當他們正在做更多的定製 – LiamHT 2014-09-30 09:39:18

0

您應該有一個ErrorController,其中包含顯示錯誤視圖的操作。和行動應具有以下注釋:

[HandleError(View = "Error", ExceptionType = typeof(Exception))] 
0

想通了,因爲它不使用基本控制器,因此該錯誤處理是拋出一個錯誤

時出現錯誤的動態內容沒有被加載