所以,我想将一些友好的错误页面应用到现有的应用程序中;由于最近的一些Azure问题太多次暴露了标准的.NET“死亡黄页”。我用这个例子: Displaying custom error CSHTML page in MVC5 on 404 / 500 / any exception?
一切似乎都运行正常(关于错误代码选择正确的视图),但结果响应是完整的HTML作为文本。该页面未呈现...这似乎适用于Chrome,Edge工作正常。我假设它不是浏览器特定的问题,而是我无意中引入的内容。
我是如何强制此页面呈现的?我错过了一个特定的“重定向”动作,而不是“重写”;我已经检查了redirectMode="ResponseRedirect".
的Web.config:
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<customErrors mode="On" defaultRedirect="~/Error" redirectMode="ResponseRedirect">
<error redirect="~/Error/HttpError404" statusCode="404"/>
<error redirect="~/Error/HttpError500" statusCode="500"/>
</customErrors>
</system.web>
ErrorController:
namespace RMS.Web.Controllers.Web
{
using System.Web.Mvc;
public class ErrorController : Controller
{
public ActionResult Error()
{
return View();
}
public ActionResult HttpError404()
{
Response.StatusCode = 200;
return View();
}
public ActionResult HttpError500()
{
Response.StatusCode = 200;
return View();
}
}
}
标准视图(每个视图都略有不同)
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<div class="error">
<div class="error-code m-b-10 m-t-20">Error 404<i class="fa fa-warning"></i></div>
<h3 class="font-bold">We couldn't find the page..</h3>
<div class="error-desc">
Sorry, but the page you are looking for was either not found or does not exist. <br />
Try refreshing the page or click the button below to go back to the Homepage.
<div>
<a class="login-detail-panel-button btn" href="/Home/Index">
<i class="fa fa-arrow-left"></i>
Go back to Homepage
</a>
</div>
<div>
@*@Model.Exception*@
</div>
</div>
</div>