【问题标题】:Custom error in mvcmvc中的自定义错误
【发布时间】:2015-03-05 06:53:01
【问题描述】:

我想在 mvc 中创建一个 Cutsum 错误。

我在 web.config 中添加自定义错误

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

在全球范围内

 protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        Server.ClearError();

        RouteData routeData = new RouteData();
        routeData.Values.Add("controller", "Error");
        routeData.Values.Add("action", "Index");
        routeData.Values.Add("exception", exception);

        if (exception.GetType() == typeof(HttpException))
        {
            routeData.Values.Add("statusCode", ((HttpException)exception).GetHttpCode());
        }
        else
        {
            routeData.Values.Add("statusCode", 500);
        }

        IController controller = new ErrorController();
        controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        Response.End();
    }

在控制器中

public class ErrorController : Controller
{
    //
    // GET: /Error/

    public ActionResult Index(int statusCode, Exception exception)
    {
        Response.StatusCode = statusCode;
        var model = new ErrorModel() {Exception = exception, HttpStatusCode = statusCode};
        return View(model);
    }

    public ActionResult NotFound()
    {
        Response.StatusCode = 404;  //you may want to set this to 200
        return View();
    }

}

在视野中

@model Behkam.Models.ErrorModel
@if (Model != null && HttpContext.Current.IsDebuggingEnabled)
{
<i class="icon-404"></i>
<h1>@Model.HttpStatusCode</h1>
<h2>خطا در دریافت اطلاعات ! </h2>
<p class="page-404">@Model.Exception.Message .@Html.ActionLink("بازگشت  به کارتابل  ", "index", "Dashboard")</p>
 }

当我在页面中遇到错误时,它会为所有异常返回错误:

传入字典的模型项的类型为“System.Web.Mvc.HandleErrorInfo”,但此字典需要类型为“Behkam.Models.ErrorModel”的模型项

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 model-view-controller custom-errors


    【解决方案1】:

    这是因为异常正在由代码中某处的 [HandleError] 属性处理(异常的堆栈跟踪或目标站点会告诉您在哪里。)

    所以你想找到并删除那个属性。 [HandleError] 创建 'System.Web.Mvc.HandleErrorInfo' 并将其传递给 \Views\Shared\Error.cshtml 作为其模型,但当然您已将错误页面的模型更改为

    @model Behkam.Models.ErrorModel
    

    因此:

    传入字典的模型项是类型 'System.Web.Mvc.HandleErrorInfo',但是这个字典需要一个模型 'Behkam.Models.ErrorModel

    类型的项目

    请注意,标准 MVC 应用程序模板执行以下操作以全局注册 [HandleErrorAttribute]

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

    所以这可能是罪魁祸首。

    【讨论】:

    • 我在 /Error/Index 中添加了错误的新视图。并为它发送 ErrorModel。而且我的项目中没有任何[HandleError] 属性。
    • 我很确定你会这样做,因为这是唯一生成“System.Web.Mvc.HandleErrorInfo”的类。
    猜你喜欢
    • 2013-11-08
    • 2013-01-10
    • 1970-01-01
    • 2015-09-21
    • 2014-11-09
    • 2010-12-24
    • 2018-12-03
    • 2010-09-27
    • 1970-01-01
    相关资源
    最近更新 更多