我在 Web.config 中设置了自定义错误启用:
<customErrors mode="On"/>
并且,在 Application_Start 中,放置这些:
protected void Application_Error(object sender, EventArgs e) {
var ex = Server.GetLastError().GetBaseException();
var routeData = new RouteData();
if (ex.GetType() == typeof(HttpException)) {
var httpException = (HttpException)ex;
var code = httpException.GetHttpCode();
routeData.Values.Add("status", code);
} else {
routeData.Values.Add("status", -1);
}
routeData.Values.Add("action", "Index");
routeData.Values.Add("controller", "Error");
routeData.Values.Add("error", ex);
IController errorController = new Kavand.Web.Controllers.ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
我还创建了一个这样的错误控制器:
public class ErrorController : Kavand.Web.Mvc.Kontroller
{
public ActionResult Index(int status, Exception error) {
Response.StatusCode = status;
HttpStatusCode statuscode = (HttpStatusCode)status;
return View(statuscode);
}
}
但是,当发生错误时,显示的是黄页,而不是我的自定义视图!大家可以帮帮我吗?非常感谢,问候