2016-03-08 79 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