我想将错误处理程序属性中的其他数据传递给错误视图。
这就是我使用属性的方式:
[ErrorHandler (View = "Error", Title = "Some title", Text = "Some text")]
public ActionResult Delete(int id, string type, DBRepository repository){...}
这是我传递数据的方式:
public class ErrorHandler : HandleErrorAttribute
{
public string Title { get; set; }
public string Text { get; set; }
/*Some other stuff*/
filterContext.Controller.TempData["CustomErrorTitle"] = Title;
filterContext.Controller.TempData["CustomErrorText"] = Text;
}
这是我的错误观点:
@model HandleErrorInfo
@{
ViewBag.Title = TempData["CustomErrorTitle"];
}
<h1 class="text-danger">@TempData["CustomErrorTitle"]</h1>
<h1 class="text-danger">@TempData["CustomErrorText"]</h1>
@if (Request.IsAuthenticated && User.IsInRole("Admin"))
{
<div>
Exception details: @Model.Exception
</div>
}
我的代码有效,但我不想使用TempData。 是否有其他方法可以在不使用TempData的情况下传递数据?
答案 0 :(得分:2)
是的,还有另外一种方式使用这样的json 在你的控制器
return Json(new
{
CustomErrorTitle= Title ,
CustomErrorText= Text
});
并在你的aspx中调用动作写
error:function(data)
{
// you can call your error attribute like this
//data.CustomErrorTitle or data.CustomErrorText
//and do what ever you want
}