我确实为MVC C#App创建了一个错误视图,它非常简单,但我可以设法显示控制器,actioin以及发生异常的消息(我需要它用于开发目的)但它总是在代码中抛出异常。这是我的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");
}
这是我的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 :(得分: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());
}
}
在Views / Shared目录中创建一个与过滤器中的ViewName匹配的视图。
答案 1 :(得分:-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();
}
注意:在上面的代码中,模型为null,表示您收到错误的原因。
这样,它会自动将错误模型发送到视图。
出处/ Usefullink:https://stackoverflow.com/a/21392400/3397630