gpt4 book ai didi

c# - Asp.Net Mvc 在来自临时数据的 View 中显示异常

转载 作者:太空宇宙 更新时间:2023-11-03 17:52:06 26 4
gpt4 key购买 nike

我正在处理 Base Controller 中的错误。我需要在 Razor View 中显示存储在临时数据中的错误,异常类型。我该怎么做?

基础 Controller 代码

protected override void OnException(ExceptionContext filterContext)
{
// if (filterContext.ExceptionHandled)
// return;

//Let the request know what went wrong
filterContext.Controller.TempData["Exception"] = filterContext.Exception.Message;

//redirect to error handler
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(
new { controller = "Error", action = "Index" }));

// Stop any other exception handlers from running
filterContext.ExceptionHandled = true;

// CLear out anything already in the response
filterContext.HttpContext.Response.Clear();
}

Razor 查看代码

<div>
This is the error Description
@Html.Raw(Html.Encode(TempData["Exception"]))
</div>

最佳答案

尝试做通用的异常属性处理,注册为全局过滤器。喜欢,

通用异常处理属性:

    /// <summary>
/// This action filter will handle the errors which has http response code 500.
/// As Ajax is not handling this error.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class HandleErrorAttribute : FilterAttribute, IExceptionFilter
{
private Type exceptionType = typeof(Exception);

private const string DefaultView = "Error";

private const string DefaultAjaxView = "_Error";

public Type ExceptionType
{
get
{
return this.exceptionType;
}

set
{
if (value == null)
{
throw new ArgumentNullException("value");
}

this.exceptionType = value;
}
}

public string View { get; set; }

public string Master { get; set; }

public void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}

if (!filterContext.IsChildAction && (!filterContext.ExceptionHandled && filterContext.HttpContext.IsCustomErrorEnabled))
{
Exception innerException = filterContext.Exception;

// adding the internal server error (500 status http code)
if ((new HttpException(null, innerException).GetHttpCode() == 500) && this.ExceptionType.IsInstanceOfType(innerException))
{
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);

// checking for Ajax request
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
var result = new PartialViewResult
{
ViewName = string.IsNullOrEmpty(this.View) ? DefaultAjaxView : this.View,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
filterContext.Result = result;
}
else
{
var result = this.CreateActionResult(filterContext, model);
filterContext.Result = result;
}

filterContext.ExceptionHandled = true;
}
}
}

private ActionResult CreateActionResult(ExceptionContext filterContext, HandleErrorInfo model)
{
var result = new ViewResult
{
ViewName = string.IsNullOrEmpty(this.View) ? DefaultView : this.View,
MasterName = this.Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData,
};

result.TempData["Exception"] = filterContext.Exception;

return result;
}
}

和错误/_错误 View

@model HandleErrorInfo
<div>
This is the error Description
@TempData["Exception"]
</div>

关于c# - Asp.Net Mvc 在来自临时数据的 View 中显示异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22208616/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com