如何捕捉在ASP.NET MVC4的Web API未定义的API方法调用 [英] How to catch undefined api method calls in ASP.NET MVC4 Web API

查看:287
本文介绍了如何捕捉在ASP.NET MVC4的Web API未定义的API方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET /单声道MVC4的Web API应用.1

ASP.NET / Mono MVC4 Web API v.1 application.

如何捕捉到未定义的API方法的调用。
调用的http://本地主机:52216 / ERP / API / undefinedmethod 返回错误到浏览器:

How to catch calls to undefined api methods. Calling http://localhost:52216/erp/api/undefinedmethod returns error to browser:

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

如何抓住这个错误记录到数据库?
我试图从问题code

How to catch this error for logging to database? I tried code from question

<一个href=\"http://stackoverflow.com/questions/15167927/how-do-i-log-all-exceptions-globally-for-a-c-sharp-mvc4-webapi-app?rq=1\">How我在全球记录所有的例外是C#MVC4的WebAPI应用?

但仍然没有执行的Application_Error和异常过滤器code,则返回出错
浏览器。
如何抓住这个错误?

but Application_Error and exception filter code is still not executed, error is returned to browser. How to catch this error ?

如果URL无API像

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

的Application_Error得到正确执行。

Application_Error is executed properly.

的WebAPI配置用于:

WebAPI configuration from VS2013 WebAPI project template is used:

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控制器使用窗体authorication,并与标准的装饰[授权]
属性。
如果授权失败,标准的XML API错误讯息

I tried changed answer. API controllers are using Forms authorication and are decorated with standard [Authorize] attribute. If authorization fails, standard xml api error message

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

发生。如何也搭上这个错误?

occurs. How to catch this error also ?

推荐答案

伊姆兰俾路支上究竟是如何做到这一点写了一篇文章。基本上你需要创建自己的HttpControllerSelector和HttpActionSelector。 你可以在这里找到的文章。

Imran Baloch wrote an article on exactly how to achieve this. Basically you need to create your own HttpControllerSelector and HttpActionSelector. You can find the article here.

编辑:

如果您的应用程序使用比在你需要做出一些改变路由WebApiConfig注册等航线。相反,在注册方法的最后确定Error404路线,定义一个新的方法(RegisterNotFound)注册的路线:

If your application uses routes other than those registered in the WebApiConfig you will need to make some changes to the routing. Instead of defining the Error404 route at the end of the Register method, define a new method (RegisterNotFound) to register the route:

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注册调用这个方法最后:

And then in the Global.asax register call this method last:

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 MVC4的Web API未定义的API方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆