【发布时间】:2014-02-07 23:52:15
【问题描述】:
我想为特定控制器显示不同的 404 页面,然后使用我现在拥有的默认值。我怎样才能做到这一点?这是我现在的设置:
protected void Application_Error()
{
if (!Request.IsLocal)
{
var context = new HttpContextWrapper(HttpContext.Current);
if (!context.Request.IsAjaxRequest())
{
context.Response.ContentType = "text/html";
var unhandledException = Server.GetLastError();
var httpException = unhandledException as HttpException;
if (httpException == null)
{
var innerException = unhandledException.InnerException;
httpException = innerException as HttpException;
}
Server.ClearError();
var routeData = new RouteData();
routeData.Values.Add("controller", MVC.Errors.Name);
if (httpException != null)
{
var httpCode = httpException.GetHttpCode();
switch (httpCode)
{
case (int) HttpStatusCode.NotFound:
routeData.Values.Add("action", "PageNotFound");
IController pageNotFoundController = new ErrorsController();
pageNotFoundController.Execute(new RequestContext(context, routeData));
break;
}
}
else
{
routeData.Values.Add("exception", unhandledException);
routeData.Values.Add("action", "Error");
IController errorController = new ErrorsController();
errorController.Execute(new RequestContext(context, routeData));
}
}
}
}
【问题讨论】:
标签: c# asp.net asp.net-mvc http-status-code-404