Tôi đã thực hiện một số thay đổi để Global.asax để tôi có thể hiển thị các trang lỗi tùy chỉnh (403, 404, và 500) Dưới đây là các mã:Trang lỗi, ném lỗi ELMAH với Custom Lỗi trang
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
//FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
protected void Application_Error(object sender, EventArgs e)
{
if (Context.IsCustomErrorEnabled)
{
ShowCustomErrorPage(Server.GetLastError());
}
}
private void ShowCustomErrorPage(Exception exception)
{
HttpException httpException = exception as HttpException;
if (httpException == null)
{
httpException = new HttpException(500, "Internal Server Error", exception);
}
Response.Clear();
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("fromAppErrorEvent", true);
switch (httpException.GetHttpCode())
{
case 403:
routeData.Values.Add("action", "AccessDenied");
break;
case 404:
routeData.Values.Add("action", "NotFound");
break;
case 500:
routeData.Values.Add("action", "ServerError");
break;
default:
routeData.Values.Add("action", "DefaultError");
routeData.Values.Add("httpStatusCode", httpException.GetHttpCode());
break;
}
Server.ClearError();
IController controller = new ErrorController();
controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}
tôi cũng đã thêm phần sau đây để Web.Config của tôi:
trang lỗi<customErrors mode="On">
<!-- There is custom handling of errors in Global.asax -->
</customErrors>
Tục xuất hiện một cách chính xác, và ELMAH sẽ đăng nhập một cách chính xác các lỗi đó là (cố tình) ném. Nhưng ELMAH cũng bắt và ghi lại một lỗi thêm:
System.InvalidOperationException: The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/account/Error.aspx ~/Views/account/Error.ascx ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Views/account/Error.cshtml ~/Views/account/Error.vbhtml ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml
bản năng đầu tiên của tôi dẫn tôi đến việc vô hiệu hóa toàn cầu HandleErrorAttribute
trong cấu hình bộ lọc. Và, các câu hỏi SO tương tự như: MVC problem with custom error pages khiến tôi tin rằng sự nghi ngờ của tôi là đúng. Nhưng ngay cả sau khi vô hiệu hóa toàn cầu HandleErrorAttribute
Tôi vẫn nhận được lỗi mà không thể tìm thấy lỗi! Đưa cái gì? chỉ linh cảm khác của tôi là điều khiển cơ sở của tôi xuất phát từ System.Web.Mvc.Controller
tôi đã cố gắng để kiểm tra các nguồn để xem nếu HandleErrorAttribute
được áp dụng cho System.Web.Mvc.Controller
nhưng không thể thu thập bất cứ điều gì ...
UPDATE: tôi đã cố gắng trọng điều khiển cơ sở của tôi để đánh dấu các trường hợp ngoại lệ như được xử lý như sau:
protected override void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
base.OnException(filterContext);
}
nhưng điều đó không giải quyết được sự cố.
UPDATE2: Tôi đã đặt tệp Error.aspx vào các chế độ xem được chia sẻ, chỉ để xem điều gì sẽ xảy ra. Khi nó ở đó, ELMAH ghi lại ngoại lệ bắt buộc và sau đó chế độ xem được chia sẻ được phân phát - nó không bao giờ đạt đến Application_Error()
.... không quá chắc chắn về những gì tạo ra nó.
), hãy chắc chắn MVC mặc định xử lý lỗi được xóa khỏi FilterConfig – solidau
Điều này thực sự khó tìm ... cảm ơn –
http: // beletsky.net/2012/11/elmahmvc-202-is-out.html "Đó là elmah.mvc.disableHandleErrorFilter. Theo mặc định, nó là' false', có nghĩa là 'HandleErrorAttribute()' từ ELMAH.MVC được sử dụng. Để tắt nó, chỉ cần thiết lập đúng với cài đặt này. " – PussInBoots