我在处理 HTTP 错误 404.0 - 未找到时遇到问题。我已经打开了
<customErrors mode="On" defaultRedirect="~/Error/General">
<error statusCode="404" redirect="~/Error/HttpError404" />
<error statusCode="500" redirect="~/Error/HttpError500" />
</customErrors>
在 Web.Config 中。但问题依然存在。我也尝试过这个解决方案(但它从来没有达到这个方法):
protected void Application_Error()
{
var exception = Server.GetLastError();
var httpException = exception as HttpException;
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Errors";
routeData.Values["action"] = "General";
routeData.Values["exception"] = exception;
Response.StatusCode = 500;
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch (Response.StatusCode)
{
case 403:
routeData.Values["action"] = "HttpError404";
break;
case 404:
routeData.Values["action"] = "HttpError404";
break;
}
}
IController errorsController = new ErrorController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorsController.Execute(rc);
}
这是控制器:
public class ErrorController : Controller
{
public ActionResult HttpError404(string error)
{
ViewData["Title"] = "Sorry, an error occurred while processing your request. (404)";
ViewData["Description"] = error;
return View("Index");
}
public ActionResult HttpError500(string error)
{
ViewData["Title"] = "Sorry, an error occurred while processing your request. (500)";
ViewData["Description"] = error;
return View("Index");
}
public ActionResult General(string error)
{
ViewData["Title"] = "Sorry, an error occurred while processing your request.";
ViewData["Description"] = error;
return this.View();
}