自定义委派处理程序特定于ASP.NET的Web API一个控制器 [英] Custom Delegating Handler specific to a Controller in ASP.NET Web API

查看:173
本文介绍了自定义委派处理程序特定于ASP.NET的Web API一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个自定义委派处理程序里面添加自定义标头的响应和放大器;检查在请求中。

I have written a Custom Delegating Handler which add custom headers to the response & checks in the request .

我添加的WebAPI配置手柄

I added the handles in WebAPi configuration

config.MessageHandlers.Add(新customHandler());

但问题是适用于所有的控制器。我需要应用特定于控制器的自定义标题。是否有可能以特定的自定义处理程序添加到一个控制器?

But the issue is applies to all the controllers. I need to apply custom header specific to a controllers. Is it possible to add custom handlers specific to a controller?

推荐答案

在这篇文章中解释了如何处理程序只适用于某些航线的末尾:<一href="http://www.asp.net/web-api/overview/working-with-http/http-message-handlers">http://www.asp.net/web-api/overview/working-with-http/http-message-handlers.您可能需要创建一个唯一的处理程序控制器为它仅适用于该控制器。

At the end of this article it explains how to apply handlers only to certain routes: http://www.asp.net/web-api/overview/working-with-http/http-message-handlers. You may have to create a unique handler for your controller for it to apply to that controller only.

    config.Routes.MapHttpRoute(
        name: "MyCustomHandlerRoute",
        routeTemplate: "api/MyController/{id}",
        defaults: new { controller = "MyController", id = RouteParameter.Optional },
        constraints: null,
        handler: HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(config), new MyCustomDelegatingMessageHandlerA());
    );

至于如何每个路由消息处理程序的管道会是什么样子,你可以看看<一href="http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-38-WebApiStackDiagramsNew/6428.ASP.NET_5F00_MVC4_5F00_WebAPI_5F00_StackDiagram_5F00_Future.jpg">here.

这篇关于自定义委派处理程序特定于ASP.NET的Web API一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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