2016-03-08 111 views
0

作爲MVC的新手,我試圖在演示應用程序中實現自定義錯誤處理。 我試過這個文檔的幾個步驟 Error Handling Document 但是錯誤404的自定義錯誤頁面沒有顯示出來。ASP.Net中的錯誤處理MVC

這是我到目前爲止嘗試過的。

我的web.config看起來像:

enter image description here

而且FilterConfig.cs樣子:

enter image description here

這裏是爲自定義樣本誤差頁面page404.html首輪測試:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
</head> 
<body> 
    <h1>Page is not there dude !!</h1> 
    Sorry about this. 
</body> 
</html> 

但是當請求一個未被配置到我的應用程序的url時,我仍然會得到一個無法找到的錯誤頁面。

enter image description here

我缺少什麼?

更新

我明白接下來我要更新錯誤頁面是.aspx頁。但現在我只想讓最初的版本起作用。

請看以下的web.config:

<?xml version="1.0"?> 

<configuration> 

    <configSections> 
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 
     <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> 
     <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> 
    </sectionGroup> 
    </configSections> 

    <system.web.webPages.razor> 
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> 
    <pages pageBaseType="System.Web.Mvc.WebViewPage"> 
     <namespaces> 
     <add namespace="System.Web.Mvc" /> 
     <add namespace="System.Web.Mvc.Ajax" /> 
     <add namespace="System.Web.Mvc.Html" /> 
     <add namespace="System.Web.Optimization"/> 
     <add namespace="System.Web.Routing" /> 
     </namespaces> 
    </pages> 
    </system.web.webPages.razor> 

    <appSettings> 
    <add key="webpages:Enabled" value="false" /> 
    </appSettings> 

    <system.web> 
    <customErrors mode="On"> 
     <error statusCode="404" redirect="~/Error/page404.html"/> <!--On all 404 pages, show page404.aspx--> 
    </customErrors> 
    <httpHandlers> 
     <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/> 
    </httpHandlers> 

    <!-- 
     Enabling request validation in view pages would cause validation to occur 
     after the input has already been processed by the controller. By default 
     MVC performs request validation before a controller processes the input. 
     To change this behavior apply the ValidateInputAttribute to a 
     controller or action. 
    --> 
    <pages 
     validateRequest="false" 
     pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" 
     pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" 
     userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 
     <controls> 
     <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" /> 
     </controls> 

    </pages> 
    </system.web> 

    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 

    <handlers> 
     <remove name="BlockViewHandler"/> 
     <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> 
    </handlers> 
    </system.webServer> 
</configuration> 
+0

請參閱自定義404錯誤頁面部分的第2部分。 – StrugglingCoder

回答

2

嘗試添加塊<customErrors><system.web>部分,像這樣:

<configuration> 
<system.web> 
    <customErrors mode="On"> 
     <error statusCode="404" redirect="~/Error/error.html" /> 
    </customErrors> 
</system.web> 
</configuration> 

[更新] 按照過去的截圖這個問題應出現如果:

  1. customerror mode is off。
  2. statuscode不匹配。

都不在你的web.config中,所以試試iisreset在服務器上。 此外,web.config附件與「view」文件夾下的web.config類似,請直接在web應用程序下嘗試web.config

+0

但該塊僅在之下。我是否要更改文件的名稱..你的意思是? – StrugglingCoder

+1

@StrugglingCoder,在屏幕截圖的customErrors是下<配置>部分直接,它應該是這樣的(I將更新回答): <結構> 通過 Midineo

+0

我已經把它回覆於,但仍然沒有運氣:(請參閱更新的屏幕截圖 – StrugglingCoder