2017-06-28 24 views
1

我已經創建了一個MVC C#應用程序的錯誤視圖,它非常簡單,但我可以設法顯示控制器,actioin和異常的消息發生(我需要它用於開發目的)但它總是在代碼中拋出異常。這是我的Global.asax顯示特定的控制器,actioin,在錯誤視圖中的異常MVC C#

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"); 
     } 

這是我ErrorrPageController

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

,這是引發錯誤的觀點,它引發的錯誤在@ 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

的可能的複製[如何使自定義錯誤頁在ASP.NET MVC 4工作(https://stackoverflow.com/questions/13905164/how-to-make-custom -error-pages-work-in-asp-net-mvc-4) –

+0

使用'try {} catch(Exception ex){}',然後重定向到頁面。你可以使用'TempData'來存儲你的異常,或者另一種存儲方法,如果你喜歡 – lloyd

回答

0

我會寫一自定義錯誤處理程序屬性並將其應用於全局。這是我寫的一個專門用來捕獲授權異常並將其發送到特定頁面的函數。主要是從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()); 
    } 
} 

創建從過濾器相匹配的VIEWNAME您的意見/共享目錄視圖註冊新的屬性。

+0

謝謝,我已經把它複製到global.asax中了嗎? –

+0

是的,我已經修改了FilterConfig,但是仍然不知道在哪裏粘貼你發佈的代碼,在我的情況下,我已經有一個控制器:ErrorPageController和一個動作:ErrorMessage,我應該在哪裏定義控制器/動作碼? –

+0

該屬性只是一個類。將其放在目錄調用/屬性中。您不需要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

相關問題