【发布时间】:2018-10-17 14:10:14
【问题描述】:
我不完全确定我的问题的标题。我需要为常见错误(404、500 等)设置自定义错误页面,但我还需要针对特定错误使用自己的“特殊”错误页面。
我配置了由 IIS 提供的自定义错误页面,如下所示,它工作正常。发生了一些事情,引发了错误 500,并提供了 /Error/InternalServerError 并保留了原始 URL。
但是如果我访问/Error/Special 我有问题。如果我将状态码设置为 500,我将获得错误页面 /Error/InternalServerError 的内容而不是内容
/Error/Special 的状态码为 500。由于 web.config,TrySkipIisCustomErrors = true 无法正常工作。
我的 web.config 中有这个:
<httpErrors errorMode="Custom" existingResponse="Replace">
<clear/>
<error statusCode="500" path="/Error/InternalServerError" responseMode="ExecuteURL" />
</httpErrors>
还有这个控制器:
public class ErrorController : Controller
{
// special error page
public ActionResult Special()
{
//Response.StatusCode = (int)HttpStatusCode.InternalServerError; // <- I cant set status code because IIS returns error page (custom error page) instead
//Response.TrySkipIisCustomErrors = true; // <- needs httpErrors - existingResponse="Passthrough" but custom error pages are not working with it
return View();
}
public ActionResult InternalServerError()
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return View();
}
}
为什么我需要“特殊”错误页面?我想做这样的事情:
public class SpecialController : Controller
{
public SpecialController()
{
// something
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if ( something != true )
{
// it would be nice to do it without redirect, just return /Error/Special and stop executing original request
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "action", "Special" },
{ "controller", "Error" }
});
}
}
}
我错过了什么吗?
【问题讨论】:
标签: c# asp.net-mvc web-config custom-error-pages custom-errors