我正在尝试在我的 MVC4 网站上配置 Magical Unicorn Mvc Error Toolkit (v 2.1.2),但我无法让它工作。这是我的代码:
网页配置
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/ServerError">
<error statusCode="404" redirect="~/Views/Error/NotFound.cshtml" />
</customErrors>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" path="~/Error/NotFound" responseMode="ExecuteURL" />
<error statusCode="500" path="~/Error/ServerError" responseMode="ExecuteURL" />
</httpErrors>
<system.webServer>
错误控制器
public class ErrorController : Controller
{
public ActionResult NotFound()
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
}
public ActionResult ServerError()
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return View();
}
}
[这些基于此https://stackoverflow.com/a/7499406/236860帖子]
CustomerErrorHandler.cs (App_Start)
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using WorldDomination.Web.Mvc;
using CustomErrors.App_Start;
[assembly: WebActivator.PreApplicationStartMethod(typeof(CustomErrorHander), "PreStart")]
namespace CustomErrors.App_Start
{
public static class CustomErrorHander
{
public static void PreStart()
{
// Register the custom error handling module.
DynamicModuleUtility.RegisterModule(typeof (CustomErrorHandlingModule));
}
}
}
我正在使用 IIS Express 在 Visual Studio 2012 中测试此应用程序。如果我尝试导航到不存在的页面,或者转到调用异常的操作方法,我会得到默认浏览器错误页面或空白页面。
我还按照ASP.NET MVC Custom Error Pages with Magical Unicorn中的建议修改了上面的代码,但这似乎没有任何区别。
谁能指出我正确的方向来完成这项工作。