要保留原始网址,你可以走的路线描述here和明确地处理内部的Global.asax而不是使用web.config中的customErrors错误或httperrors部分(仍然可以配置为回退),但改变 的IHttpHandler.ProcessRequest部分链接的网站示例与HttpContext.Server.TransferRequest(路径,true)。 我实现Application_Error事件像这样在我的项目之一:
protected void Application_Error()
{
Exception error = Server.GetLastError();
var code = (error is HttpException) ? (error as HttpException).GetHttpCode() : 500;
if (!Context.IsDebuggingEnabled
&& code != 404)
{
// persist error to error log db
//Log.Logger.Error("unhandled exception: ", exception);
}
if (WebConfigurationManager.AppSettings["EnableCustomExceptionPage"].Equals("false"))
{
return;
}
Response.Clear();
Server.ClearError();
string path = this.Request.Path;
string action;
switch (code)
{
case 401:
action = "Unauthorized";
break;
case 403:
action = "Forbidden";
break;
case 404:
action = "NotFound";
break;
default:
action = "Generic";
break;
}
string newPath = string.Format("~/Error/{0}?source={1}&message={2}", action, path,
HttpUtility.UrlEncode(error.Message));
Context.Server.TransferRequest(newPath, true);
}
内部,它要求一个新的路径传输办理了由ErrorController在描述上面的例子,其行为可能是这样的,即这样的:
public ViewResult Generic(string source, string message)
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 500;
ViewBag.Source = source;
ViewBag.Message = HttpUtility.UrlDecode(message);
return View();
}
public ViewResult NotFound(string source, string message)
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 404;
ViewBag.Source = source;
ViewBag.Message = HttpUtility.UrlDecode(message);
return View();
}
...
TrySkipIisCustomErrors = true
阻止IIS重定向到默认的自定义错误页面。 您可以在Application_Error覆盖方法内,以不同于HttpException的方式处理抛出的异常(如业务/服务层中抛出的特殊异常)。
只是可以肯定的。你说“customError状态代码10到我的Web.config”这应该是410对吗?如果是,你可以更新这个问题吗? – Spock
我认为你需要这个别名。看看这里http://stackoverflow.com/questions/9853429/how-do-i-create-an-alias-for-a-page-url –
谢谢拉杰,我已经更新了这个问题。并且非常感谢Matt,我会研究别名。 – valin077