2017-06-28 5 views
1

MVC C#アプリケーションのエラービューを作成しましたが、これは非常に簡単ですが、コントローラ、actioin、例外のメッセージを表示できます(私はデベレップメントの目的のために必要です)しかし、それは常にコード内で例外をスローします。これは、これは私のErrorrPageControllerエラービューで特定のコントローラ、actioin、例外を表示するMVC C#

public class ErrorPageController : Controller 
    { 
     public ActionResult ErrorMessage() 
     { 
      return View(); 
     } 
    } 

である私のGlobal.asax

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

     protected void Application_Error(object sender, EventArgs e) 
     { 
      Exception exc = Server.GetLastError(); 
      Server.ClearError(); 
      Response.Redirect("/ErrorPage/ErrorMessage"); 
     } 

であり、これはエラーをスロー図であり、それは@ Model.ControllerName、@ Model.ActionNameと@Modelにエラーがスローされます.Exception.Message

@model System.Web.Mvc.HandleErrorInfo 
<div class="container"> 

    <div class="row"> 
     <div class="col-md-6 col-md-offset-3"> 
      <div> 
       <br /> 
       <div class="form-group"> 
        <div class="row"> 
         <div class="col-md-12"> 
          <img src="~/Imagenes/logo.png" class="img-responsive center-block" /> 
         </div> 
        </div> 
        <h2>Ooops an error has been triggered</h2> 
        <p>Controller = @Model.ControllerName</p> 
        <p>Action = @Model.ActionName</p> 
        <p>Message = @Model.Exception.Message</p> 
       </div> 
       @*<hr />*@ 
       <br /> 


       <div class="form-group"> 
        <div class="col-md-12"> 
         <a href="@Url.Action("TipoEvento", "Home")" class="pull-right linkedin-link">Regresar <i class="fa fa-angle-right"></i></a> 
        </div> 
       </div> 

      </div> 

     </div> 
    </div> 
</div> 

、これは

をスローエラーです

しかし、私は本当にあなたが私を助けると

+0

可能な重複(https://stackoverflow.com/questions/13905164/how-to-make-custom [カスタムエラーページがASP.NET MVC 4で動作させる方法] -error-pages-work-in-asp-net-mvc-4) –

+0

'try {} catch(Exception ex){}'を使ってページにリダイレクトします。あなたの例外を格納するために 'TempData'を使うことができます。あるいは、もしあなたが好きであれば格納するための別のメソッドです。 – lloyd

答えて

0

私が書きたいエラーページに詳細なエラー情報を表示する方法を教えてください可能性があり、(DEV目的のために、再び、)それらの情報を表示する必要があるので、カスタムのエラーハンドラ属性を適用し、それをグローバルに適用します。ここでは、特に許可例外をトラップして特定のページに送信するように書いたものがあります。主なものは、ExceptionContextからアクションとコントローラの情報を取得することです。

public class HandleUnauthorizedAttribute : HandleErrorAttribute 
{ 
    public override void OnException(ExceptionContext filterContext) 
    { 
     base.OnException(filterContext); 

     //remove the following line to capture all exceptions. this only lets Security exceptions through 
     if (filterContext.Exception.GetType() != typeof(SecurityException)) return; 

     var controllerName = (string)filterContext.RouteData.Values["controller"]; 
     var actionName = (string)filterContext.RouteData.Values["action"]; 
     var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); 

     filterContext.Result = new ViewResult 
     { 
      //name your view whatever you want and place a matching view in /Views/Shared 
      ViewName = "Unauthorized", 
      ViewData = new ViewDataDictionary<HandleErrorInfo>(model), 
      TempData = filterContext.Controller.TempData 
     }; 
     filterContext.ExceptionHandled = true; 
     filterContext.HttpContext.Response.Clear(); 
     filterContext.HttpContext.Response.StatusCode = 403; 
     filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
    } 
} 

FilterConfig.cs

public class FilterConfig 
{ 
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     filters.Add(new HandleUnauthorizedAttribute()); 
    } 
} 

で新しい属性があなたの閲覧/フィルタからビュー名に一致した共有ディレクトリにビューを作成して登録します。

+0

ありがとう、私はこれをglobal.asaxでコピーしますか? –

+0

はい、私はすでにFilterConfigを修正しましたが、あなたが投稿したコードを貼り付ける場所はわかりません。私の場合はコントローラがあります:ErrorPageControllerとAction:ErrorMessage、どこにコントローラ/アクションを定義する必要がありますか?コード? –

+0

属性は単なるクラスです。それをディレクトリコール/ Attributesにドロップします。 ErrorPageControllerまたはアクションは必要ありません。 ActionFilterはあなたをビューに送ります。 – Fran

-1

こんにちはそのあなただけのアプリケーションの開始に以下のようなエントリを追加する必要があるようです:

protected void Application_Start() 
    {  
     AreaRegistration.RegisterAllAreas(); 

     //Here is the entry 
     RegisterGlobalFilters(GlobalFilters.Filters); 


     RegisterRoutes(RouteTable.Routes); 

     ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder(); 
    } 

注:では、あなたのコードの上にモデルを使用すると、エラーを得た理由という帰でした。

このようにして、エラーモデルが自動的にビューに送信されます。

ソース/ Usefullink:https://stackoverflow.com/a/21392400/3397630

関連する問題