Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Delegating Handler specific to a Controller in ASP.NET Web API

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

I added the handles in WebAPi configuration

config.MessageHandlers.Add(new 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?

like image 738
user1135534 Avatar asked Nov 29 '12 15:11

user1135534


1 Answers

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());
    );

Regarding how the pipeline of per-route message handlers would look like, you can look here.

like image 99
Nick Avatar answered Oct 15 '22 16:10

Nick