Tôi đang cố định cấu hình Bộ công cụ Lỗi Magical Unicorn Mvc (v 2.1.2) trên trang web MVC4 của tôi nhưng tôi không thể làm cho nó hoạt động. Dưới đây là mã của tôi:Cấu hình Bộ công cụ Lỗi Magical Unicorn Mvc
Web.config
<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>
Lỗi điều khiển
public class ErrorController : Controller
{
public ActionResult NotFound()
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
}
public ActionResult ServerError()
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return View();
}
}
[Chúng được dựa trên https://stackoverflow.com/a/7499406/236860 bài này]
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));
}
}
}
Tôi đang thử nghiệm ứng dụng này trong Visual Studio 2012 bằng IIS Express. Nếu tôi cố gắng điều hướng đến trang không tồn tại hoặc chuyển sang phương thức hành động gọi là ngoại lệ, tôi sẽ nhận được trang lỗi trình duyệt mặc định hoặc trang trống.
Tôi cũng đã sửa đổi mã ở trên như được đề xuất tại ASP.NET MVC Custom Error Pages with Magical Unicorn nhưng điều này dường như không có bất kỳ sự khác biệt nào.
Bất kỳ ai cũng có thể chỉ cho tôi đúng hướng để làm việc này.
Sau khi lãng phí thời gian với câu trả lời cho http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc, hóa ra đây là cách tiếp cận tuyệt vời mà chỉ đơn giản là công trình. –
Cảm ơn bạn Michael, vui vì nó giúp đỡ. – Neilski