2016-12-14 81 views
0

自定义错误页我想实现一个自定义错误页,显示我自己的错误消息,而不是显示默认的错误消息。如何实现在asp.net的MVC

,所以我已在web.config文件中的一些变化。这里是我的web.config文件

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=301880 
    --> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <appSettings> 
    <add key="webpages:Version" value="3.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5.2" /> 
    <httpRuntime targetFramework="4.5.2" /> 
    <httpModules> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
    </httpModules> 
    <authentication mode="Forms"> 
     <forms loginUrl="Account/Login"> 
     </forms> 
    </authentication> 
    <customErrors mode="On" defaultRedirect="Error.cshtml" > 
     <error statusCode="404" redirect="~/Error.cshtml" /> 
    </customErrors> 
    </system.web> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
     <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
    <system.webServer> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules> 
     <remove name="ApplicationInsightsWebTracking" /> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
    </modules> 
    </system.webServer> 
    <connectionStrings> 
    <add name="ShoppersEntities" connectionString="metadata=res://*/Context.Shoppers.csdl|res://*/Context.Shoppers.ssdl|res://*/Context.Shoppers.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;initial catalog=Shoppers;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
    <add name="ShoppersEntities1" connectionString="metadata=res://*/Context.Shoppers.csdl|res://*/Context.Shoppers.ssdl|res://*/Context.Shoppers.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;initial catalog=Shoppers;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 

以下是我Error.cshtml文件

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 
    <title>Home | E-Shopper</title> 
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" /> 
    <link href="~/Content/font-awesome.min.css" rel="stylesheet" /> 
    <link href="~/Content/prettyPhoto.css" rel="stylesheet" /> 
    <link href="~/Content/price-range.css" rel="stylesheet" /> 
    <link href="~/Content/animate.css" rel="stylesheet" /> 
    <link href="~/Content/main.css" rel="stylesheet" /> 
    <link href="~/Content/responsive.css" rel="stylesheet" /> 
    <!--[if lt IE 9]> 
    <script src="js/html5shiv.js"></script> 
    <script src="js/respond.min.js"></script> 
    <![endif]--> 
    <link rel="shortcut icon" href="~/Content/images/ico/favicon.ico" /> 
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="~/Content/images/ico/apple-touch-icon-144-precomposed.png" /> 
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="~/Content/images/ico/apple-touch-icon-114-precomposed.png" /> 
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="~/Content/images/ico/apple-touch-icon-72-precomposed.png" /> 
    <link rel="apple-touch-icon-precomposed" href="~/Content/images/ico/apple-touch-icon-57-precomposed.png" /> 
</head> 
<!--/head--> 
<body> 
    <div class="container text-center"> 
     <div class="logo-404"> 
      <a href="@Url.Action("Index", "Home")"><img src="~/Content/images/home/logo.png" alt="logo" /></a> 
     </div> 
     <div class="content-404"> 
      <img src="~/Content/images/404/404.png" class="img-responsive" alt="" /> 
      <h1><b>OPPS!</b> We Couldn’t Find this Page</h1> 
      <p>Uh... So it looks like you brock something. The page you are looking for has up and Vanished.</p> 
      <h2><a href="@Url.Action("Index", "Home")">Bring me back Home</a></h2> 
     </div> 
    </div> 
    <script src="~/Scripts/jquery.js"></script> 
    <script src="~/Scripts/price-range.js"></script> 
    <script src="~/Scripts/jquery.scrollUp.min.js"></script> 
    <script src="~/Scripts/bootstrap.min.js"></script> 
    <script src="~/Scripts/jquery.prettyPhoto.js"></script> 
    <script src="~/Scripts/main.js"></script> 
</body> 
</html> 

我的错误页面存储在文件夹视图和该共享文件夹。 请帮我显示自定义错误页。

+0

我收到称为运行时错误“而处理您的请求时发生异常。另外,在执行自定义错误页的第一个异常又发生了异常。该请求已被终止。” –

+0

使用Visual Studio调试模式下,交未处理的异常,你已经到达那里。也'的defaultRedirect =“Error.cshtml”'是不调用CSHTML文件一个正确的事,需要定义误差控制器然后使用虚拟路径调用定制错误动作方法(例如'的defaultRedirect =“〜/错误”')。 –

+0

谢谢。我通过添加名为ErrorController的新控制器并在其中保存了一个方法来完成此操作。该方法调用我的customErrror页面。现在它工作的404错误。 –

回答

3

从给定web.config文件,似乎这些线触发第二异常而返回定义错误页:

<customErrors mode="On" defaultRedirect="Error.cshtml" > 
    <error statusCode="404" redirect="~/Error.cshtml" /> 
</customErrors> 

由于defaultRedirect属性只接受一个HTML文件或虚拟/相对路径的控制器动作任一绝对路径到CSHTML文件的方法,直接绝对路径是不合适的,由于它里面剃刀码应使用的操作方法的服务器被变换为HTML存在。

这个问题可以通过这样的默认操作方法创建一个控制器类来解决(我假定目标的错误页面存在于/查看/共享目录):

public class ErrorController : Controller 
{ 
    public ViewResult Index() 
    { 
     return View("Error"); 
    } 
} 

然后,替换defaultRedirect走错了路使用正确的虚拟路径属性:

<customErrors mode="On" defaultRedirect="~/Error" > 
    <error statusCode="404" redirect="~/Error" /> 
</customErrors> 

PS:您可以通过添加操作方法,包括Response.StatusCode = [HTML status code]ErrorController实现超过404其他状态代码自定义错误页。