我在 Azure 上有一个 MVC 网站。我编写了一个代表资源的 Controller 操作,该操作应该返回 HTTP 404,但正文内容应该是一些 HTML,我在其中解释了 404 的原因。这是作为设置 Response.StatusCode
的标准操作来实现的。 。这在本地工作正常,但是当部署到 Azure 时,我没有得到自定义 View ,而只是得到纯文本的错误消息。我已经删除了<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 时呈现 View ?
最佳答案
我可以通过将此 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
关于asp.net-mvc - Azure 网站中的 404 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18291550/