2013-10-18 29 views
0

我正在爲「Gone」請求(410代碼)創建一個Controller和View,並且我已經完成此操作的方式是將customError狀態代碼410添加到我的Web.config中。URL如何保持原樣但仍呈現錯誤視圖?

這妥善處理異常,並呈現視圖,但響應URL是

http://www.mysite.com/Error/Gone?aspxerrorpath=/requested-page

我希望保留原來的URL,同時還顯示了飄查看,換句話說,離開網址原因是:「http://www.mysite.com/requested-page

任何想法,我可以開始呢?

+0

只是可以肯定的。你說「customError狀態代碼10到我的Web.config」這應該是410對嗎?如果是,你可以更新這個問題嗎? – Spock

+0

我認爲你需要這個別名。看看這裏http://stackoverflow.com/questions/9853429/how-do-i-create-an-alias-for-a-page-url –

+0

謝謝拉傑,我已經更新了這個問題。並且非常感謝Matt,我會研究別名。 – valin077

回答

0

要保留原始網址,你可以走的路線描述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的方式處理拋出的異常(如業務/服務層中拋出的特殊異常)。

相關問題