asp.net - 如何在 ASP.NET MVC4 Web API 中捕获未定义的 api 方法调用

标签 asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api

ASP.NET/Mono MVC4 Web API v.1 应用程序。

如何捕获对未定义 api 方法的调用。 调用 http://localhost:52216/erp/api/undefinedmethod 向浏览器返回错误:

<Error>
<Message>
No HTTP resource was found that matches the request URI 'http:// localhost:52216/erp/api/undefinedmethod'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'undefinedmethod'.
</MessageDetail>
</Error>

如何捕获此错误以记录到数据库? 我尝试了问题中的代码

How do I log ALL exceptions globally for a C# MVC4 WebAPI app?

但是Application_Error和异常过滤代码仍然没有执行,错误返回到 浏览器。 如何捕捉这个错误?

如果 url 没有像 api 这样的

'http://localhost:52216/erp/undefinedmethod'

Application_Error 正确执行。

使用 VS2013 WebAPI 项目模板中的 WebAPI 配置:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

 }

更新

我尝试更改答案。 API Controller 使用表单授权并用标准[Authorize]装饰 属性。 如果授权失败,标准xml api错误消息

<Error>
<Message>Authorization has been denied for this request.</Message>
</Error>

发生。如何捕获此错误?

最佳答案

Imran Baloch 写了一篇关于如何实现这一目标的文章。基本上,您需要创建自己的 HttpControllerSelector 和 HttpActionSelector。 You can find the article here .

编辑:

如果您的应用程序使用的路由不是 WebApiConfig 中注册的路由,您将需要对路由进行一些更改。不要在 Register 方法末尾定义 Error404 路由,而是定义一个新方法 (RegisterNotFound) 来注册该路由:

public static class WebApiConfig
{    
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

    public static void RegisterNotFound(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "Error404",
            routeTemplate: "{*url}",
            defaults: new { controller = "Error", action = "Handle404" }
        );
    }
}

然后在Global.asax寄存器中最后调用这个方法:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    WebApiConfig.RegisterNotFound(GlobalConfiguration.Configuration);


    GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), 
        new HttpNotFoundAwareDefaultHttpControllerSelector(GlobalConfiguration.Configuration));
    GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpActionSelector), 
        new HttpNotFoundAwareControllerActionSelector());

}

关于asp.net - 如何在 ASP.NET MVC4 Web API 中捕获未定义的 api 方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20212725/

相关文章:

c# - 如何为页面中的多个表格应用相同的样式

ASP.Net MVC 3 - 密码保护 View

html - ASP.NET MVC 和 HTML5

c# - 我如何在单个 Controller 中拥有多个 GET 方法

jquery - Ajax 调用未调用服务器端,并且在 httpfox 中在 ajax post 调用中显示错误为 "Error loading content (NS_ERROR_DOCUMENT_NOT_CACHED)"

javascript - 如何使用javascript获取复选框项目的值

javascript - 使用 MVC 5 将图像发送到客户端并在新窗口或选项卡中打开

c# - 如何在 ASP.NET WebAPI 项目中使用 MVC 路由

c# - 特定 ASP.NET MVC4 URL 的自定义配置设置

asp.net - ASP.NET Web 应用程序上忽略其他文化的全局资源