MVC中Http错误405的自定义错误处理程序 [英] Custom Error Handler for Http Error 405 in MVC
问题描述
我似乎无法弄清楚如何自定义处理这个错误。我有一个端点定义如下:
I can't seem to figure out how to custom handle this error. I have a endpoint defined like this:
[HttpGet]
public ActionResult Test(){...}
[HttpGet] public ActionResult Test() { ... }
如果我尝试POST到这个端点,它会抛出正确的http 405错误。问题是我想提供自定义错误页面,而不是IIS默认页面。我试过在global.asax中的application_error方法中打破,但是从来没有被调用过,我已经尝试在web.config中添加了< customErrors />
部分但无济于事
If I try to POST to this endpoint it throws the http 405 error which is correct. The problem is I want to provide a custom error page and not the IIS default one. I've tried breaking in the application_error method in global.asax but it never get's called, and I've tried adding the <customErrors />
section in web.config but to no avail.
任何建议?
推荐答案
您是否使用MVC3?如果是,请注意
Are you using MVC3? If so, watch out for
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
在您的global.asax中。
in your global.asax.
此过滤器将捕获控制器中抛出的任何异常并使其成为500响应代码。然后会尝试找到一个名为Error的视图。评论此内容,customErrors将按照您的期望工作。
This filter will catch any exception thrown in a controller and make it a 500 response code. It will then try and find a View called "Error". Comment this out and customErrors will work as you expect.
同时,双重检查您正在使用的操作方法或控制器上的[HandleError]属性。
Also, double check for [HandleError] attributes on the action method or controller you are using.
这篇关于MVC中Http错误405的自定义错误处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!