2013-02-28 27 views
5

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.

Trả lời

14

Cuối cùng, tôi không thể sử dụng Bộ công cụ Lỗi Magical Unicorn Mvc để hoạt động. Tin tốt là tôi không nghĩ mình phải làm vậy! Vì tôi đang triển khai ứng dụng MVC vào một máy chủ web IIS 7.5, tôi có thể sử dụng phần system.webServer.httpErrors sau của Web.config của tôi và một bộ điều khiển lỗi tùy chỉnh.

Web.Config

<system.web> 
    <httpRuntime targetFramework="4.5" /> 
    <compilation debug="false" targetFramework="4.5"> 
    <customErrors mode="Off" /> <!-- IMPORTANT --> 
    ... 
</system.web> 

<system.webServer> 
    <httpErrors errorMode="Custom" existingResponse="Replace"> 
     <remove statusCode="403" /> 
     <error statusCode="403" responseMode="ExecuteURL" path="/Error/AccessDenied" /> 
     <remove statusCode="404" /> 
     <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" /> 
     <remove statusCode="500" /> 
     <error statusCode="500" responseMode="ExecuteURL" path="/Error/ApplicationError" /> 
    </httpErrors> 
    ... 
</system.webServer> 

Lỗi điều khiển

public class ErrorController : Controller 
{ 
    public ActionResult AccessDenied() 
    { 
     Response.StatusCode = (int)HttpStatusCode.Forbidden; 
     Response.TrySkipIisCustomErrors = true; 

     if (Request.IsAjaxRequest()) 
     { 
     // return Json friendly response here 
     } 

     return View(); 
    } 

    public ActionResult NotFound() 
    { 
     Response.StatusCode = (int)HttpStatusCode.NotFound; 
     Response.TrySkipIisCustomErrors = true; 

     if (Request.IsAjaxRequest()) 
     { 
     // return Json friendly response here 
     } 

     return View(); 
    } 

    public ActionResult ApplicationError() 
    { 
     Response.StatusCode = (int)HttpStatusCode.InternalServerError; 
     Response.TrySkipIisCustomErrors = true; 

     if (Request.IsAjaxRequest()) 
     { 
     // return Json friendly response here 
     } 

     return View(); 
    } 
} 
  • này tất cả dường như làm việc tốt với IIS Express và IIS 7.5.
  • Elmah ghi lại các lỗi mà không có bất kỳ thay đổi nào đối với các bộ lọc lỗi mặc định.
  • Fiddler cũng gợi ý rằng các mã trạng thái HTTP chính xác cũng được duy trì chính xác.
+0

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. –

+0

Cảm ơn bạn Michael, vui vì nó giúp đỡ. – Neilski

Các vấn đề liên quan