Asp.net-Mvc

Azure 網站中的 404 處理

  • September 22, 2013

我在 Azure 上有一個 MVC 網站。我已經編寫了一個控制器操作來代替資源,它應該返回 HTTP 404,但主體內容應該是一些 HTML,我在其中解釋了 404 的原因。這是作為設置Response.StatusCode. 這在本地工作得很好,但是當部署到 Azure 時,我沒有得到我的自定義視圖,而只是一個純文字的錯誤消息。我已<customErrors>在 Azure 上刪除以進行調試,結果相同。

這是部署到 Azure 時收到的原始響應:

HTTP/1.1 404 Not Found
Cache-Control: private
Content-Length: 103
Content-Type: text/html
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By: ARR/2.5
X-Powered-By: ASP.NET
Date: Sat, 17 Aug 2013 17:24:19 GMT

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

同樣重要的是,如果我刪除了提供此服務的路由,我會得到一個標準的 .NET 404 錯誤頁面,所以我猜我的自定義操作正在執行。動作很簡單:

   [HttpGet]
   public ActionResult LegacyMedia(string originalUrl)
   {
       ViewBag.OriginalUrl = originalUrl;
       return new ViewResult404() {ViewName = "LegacyMedia"};
   }

   public class ViewResult404 : ViewResult
   {
       public override void ExecuteResult(ControllerContext context)
       {
           context.HttpContext.Response.StatusCode = (int) HttpStatusCode.NotFound;
           base.ExecuteResult(context);
       }
   }

如何在 Azure 上響應 HTTP 狀態 404 時呈現我的視圖?

我可以通過將此 httpErrors 條目添加到 web.config 來解決此問題:

<configuration>  
 <system.webServer>  
   <httpErrors existingResponse="PassThrough"/>  
 </system.webServer>  
<configuration>

我在這裡找到了這個答案:

http://blog.dezfowler.com/2010/09/aspnet-custom-error-not-shown-in-azure.html

引用自:https://stackoverflow.com/questions/18291550