我终于能够重定向404和500错误,但是当他们这样做时,他们会给我两个结果中的一个。
当我使用它时(特别是404s):
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<remove statusCode="500"/>
<error statusCode="404" path="/Home/PageNotFound" responseMode="ExecuteURL" />
<error statusCode="500" path="/Home/InternalServerError" responseMode="ExecuteURL" />
</httpErrors>
输入一个不存在的链接(例如localhost:11111 / yo),我得到一个白页,上面没有任何内容。
当我使用它时(特别是404s):
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<remove statusCode="500"/>
<error statusCode="404" path="/Home/PageNotFound" responseMode="File" />
<error statusCode="500" path="/Home/InternalServerError" responseMode="File" />
</httpErrors>
输入一个不存在的链接(例如localhost:11111 / yo),我在白页上得到这行文字:
您要查找的资源已被删除,名称已更改或暂时无法使用。
我在两个错误页面中都放置了断点,但两个示例中都没有出现
以下是我的错误页面:
public ActionResult PageNotFound()
{
Response.StatusCode = 404;
return View();
}
public ActionResult InternalServerError()
{
Response.StatusCode = 500;
return View();
}
它们位于HomeController内部,因此path="/Home/...
如何使它们在发生错误时命中(我假设404的解决方案对于500来说是相同的)。我在404和500页面上写了一段说“嗨”。这就是我知道它会起作用的方式。
我也使用斜杠尝试了这个解决方案,但它不起作用web.config errors fail with responseMode="File"
答案 0 :(得分:0)
Web.config设置
<system.web>
<customErrors mode="On" defaultRedirect="~/Error">
<error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>
</system.web>
ErrorController
public class ErrorController : Controller
{
public ViewResult Index()
{
return View("Error");
}
public ViewResult NotFound()
{
Response.StatusCode = 404; //you may want to set this to 200
return View("NotFound");
}
}
Error.cshtml页面
@model System.Web.Mvc.HandleErrorInfo
@{
Layout = "_Layout.cshtml";
ViewBag.Title = "Error";
}
<div class="list-header clearfix">
<span>Error</span>
</div>
<div class="list-sfs-holder">
<div class="alert alert-error">
An unexpected error has occurred. Please contact the system administrator.
</div>
@if (Model != null && HttpContext.Current.IsDebuggingEnabled)
{
<div>
<p>
<b>Exception:</b> @Model.Exception.Message<br />
<b>Controller:</b> @Model.ControllerName<br />
<b>Action:</b> @Model.ActionName
</p>
<div style="overflow:scroll">
<pre>
@Model.Exception.StackTrace
</pre>
</div>
</div>
}
</div>