2016-12-14 25 views
0

デフォルトのエラーメッセージを表示する代わりに私自身のエラーメッセージを表示するカスタムエラーページを実装したいと思います。asp.net mvcでカスタムエラーページを実装する方法

私は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> 

私のエラーページがビューに格納されているファイルフォルダとその共有フォルダ内にある以下の私のweb.configファイルです。 カスタムエラーページを表示させてください。与えられたweb.configファイルから

+0

私はと呼ばれるランタイムエラーを取得しています「リクエストの処理中に例外が発生しました。最初の例外のカスタムエラーページを実行しながらさらに、別の例外が発生しました。リクエストが終了した。」 –

+0

使用Visual Studioのデバッグモード、ポストをあなたが手に入れている未処理の例外。また 'defaultRedirect =" Error.cshtml "'はcshtmlファイルを呼び出すのに適切ではありません。エラー・コントローラを定義してから、カスタム・エラー・アクション・メソッドを呼び出すために仮想パスを使用する必要があります(例えば 'defaultRedirect ="〜/ Error "')。 –

+0

ありがとうございます。私はErrorControllerという新しいコントローラーを追加してそれを行い、その中に1つのメソッドを保持しました。そのメソッドは私のcustomErrorページを呼び出します。今は404エラーのために働いています。 –

答えて

3

、カスタムエラーページを戻しながら、これらの行は、2番目の例外をトリガするようだ:

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

defaultRedirect属性のみHTMLファイルへの絶対パスまたはコントローラのアクションへの仮想/相対パスのどちらかを受け入れているので方法では、CSHMLファイルへの直接絶対パスは、その中にRazorコードが存在するため、アクションメソッドを使用してサーバーにHTMLに変換する必要があるため不適切です。 defaultRedirectに間違ったパスを置き換え、その後

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

問題は、このようなデフォルトのアクションメソッドでコントローラクラスを作成することによって解決することができます(私は、ターゲットのエラーページが/閲覧/共有ディレクトリ内に存在すると仮定しました)正しい仮想パスと属性:

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

PS:あなたはErrorController内部Response.StatusCode = [HTML status code]を含めるアクションメソッドを追加することにより、404以外のステータスコードについてカスタムエラーページを実装することができます。

関連する問題