Intercept object arrives Controller

167 views Asked by At

I have a controllers with GET/POST methods and i was wondering if it's possible to intercept the object before reach the POST method on Controller.

Here is my method on Controller:

[Route("{type}")]
[HttpPost]
public HttpResponseMessage Save(string type, [FromBody] Message message)
{
    ....
    return Request.CreateResponse((HttpStatusCode)200, result);
}

Is possible to intercept the object Message before method Save() has been called? I've created a DelegatingHandler but it's not working.

Here is how i've added the route:

IHttpRoute route = GlobalConfiguration.Configuration.Routes.CreateRoute(
 routeTemplate: "api/message/{type}",
 defaults: new HttpRouteValueDictionary("route"),
 constraints: null,
 dataTokens: null,
 handler: new ValidationHandler());
GlobalConfiguration.Configuration.Routes.Add("MyRoute", route);

Any ideas how can i do it?

2

There are 2 answers

4
hutchonoid On BEST ANSWER

If you have already created your DelegatingHandler you will need to configure it as follows:

GlobalConfiguration.Configuration
                  .MessageHandlers
                  .Add(new FooHandler());
4
ivamax9 On

Yes, it can be done within a web proxy after the request has been made or you also can add some middleware in pipeline. But if I understand you right, you also can do it simply without handlers, via custom binder which will change your Message object.

How to bind to custom objects in action signatures in MVC/WebAPI