我希望在我的应用程序中具有 401、404 等错误代码的自定义错误页面。
我尝试了但不起作用? 在 Web.config 中
<customErrors mode="Off" /> //Under system.web
<httpErrors errorMode="Custom" existingResponse="Replace" >//Under system.webServer
<clear />
<remove statusCode="401"/>
<error statusCode="401" responseMode="ExecuteURL" path="/Error/Unauthorized" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error" />
</httpErrors>
我还创建了错误 Controller 和未经授权的 View 。
这怎么行?
最佳答案
示例:
web.config:
在system.web
中添加
<customErrors mode="RemoteOnly" defaultRedirect="~/error">//RemoteOnly means that on local network you will see real errors
<error statusCode="401" path="~/Error/Unauthorized" />
<error statusCode="404" path="~/Error/NotFound" />
<error statusCode="500" path="~/Error" />
</customErrors>
在system.webServer
中添加
<httpErrors errorMode="Detailed" />
Controller : 你的 Controller 类似于
public class ErrorController : Controller
{
public ViewResult Index()
{
return View("Error");
}
public ViewResult NotFound()
{
Response.StatusCode = 404;
return View("NotFound");
}
}
查看: 你的看法类似于
@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>
}
</div>
希望对您有帮助。
关于asp.net-mvc - MVC ASP NET 中针对所有错误的自定义错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43973382/