要保留原始網址,你可以走的路線描述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