问题描述
我要一个标识符传递到ASP.NET MVC自定义错误页。
I want to pass an identifier to a custom error page in ASP.NET MVC.
我有web.config中设置重定向到一个错误页面时,有一个状态500错误。我不知道如何从应用code抛出异常传递一个错误的ID(还设置500 HTTP状态)返回到所配置的错误页面。
I have the web.config set up to redirect to an error page when there is an status 500 error. I don't know how to pass an error id from the application code that throws the exception (also sets HTTP Status of 500) back to the error page that is configured.
如果我只是用状态200并返回从控制器的错误方法的示意图,并在那里做了所有的逻辑,但其状态code必须是500,所以这不是一个可接受的解决方案,这很容易。
It's easy if I just used Status 200 and returned a view from the Error method of the controller and did all the logic in there, however the Status code must be 500, so that's not an acceptable solution.
我也没有和这不会使用会话。我想使用的查询字符串或查看数据。
I also do not and will not use session for this. I'd like to use query string or view data.
不重复。这是关于错误处理(状态code 500)和异常。 404大约是找不到页面
推荐答案
我不知道定制信息的更好的方式来错误的web.config配置的页面。这是当我需要一个错误重定向我通常做的:
I don't know a better way to customized information to error page configured in web.config. This is what I normally do when i need an error redirection:
我将创建一个错误处理行为过滤器:
I'll create an error handler action filter:
public class MyErrorHandler: HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception != null)
{
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
filterContext.ExceptionHandled = true;
filterContext.Result = new RedirectResult("/error?error_id=123456", true);
return;
}
base.OnException(filterContext);
}
}
中的异常过滤器,你可以尝试处理目前存在的误差不同的错误标识,或任何你preFER改变例外。
Within the Exception filter, you may try to handle differnet error with different error id, or whatever you prefer to alter the exception.
然后应用到它的控制器或CONTROLER类:
Then apply it onto controller or controler class:
[MyErrorHandler]
public class HomeController : Controller
{
....
}
,你会得到一个HTTP状态500和将被重定向到:
And you will get a Http status 500 and will be redirected to:
http://<host>/error?error_id=123456
这篇关于传入ASP.NET MVC的价值定义错误页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!