0

In previous version of the WebApi you could do the following:

RouteTable.Routes.MapServiceRoute<UserService>("1.0/User/", defaultWebApiConfiguration);
RouteTable.Routes.MapServiceRoute<SomeOtherService>("1.0/SomeOtherService/", largeFilesConfig);

This would allow you to have different message handlers on different services. This is apparently not possible in the new framework: ASP.NET MVC 4 WebApi Support For Multiple HttpConfigurations

Alternatively I had projects where I edited the RequestHandlers in the WebApiConfiguration to add handlers if certain attributes existed like this:

    public static void AppendAuthorizationRequestHandlers(
  this WebApiConfiguration config)
{
  var requestHandlers = config.RequestHandlers;
  config.RequestHandlers = (c, e, od) =>
  {
    if (requestHandlers != null)
    {
      requestHandlers(c, e, od); // Original request handler
    }
    var authorizeAttribute = od.Attributes.OfType<RequireAuthorizationAttribute>()
      .FirstOrDefault();
    if (authorizeAttribute != null)
    {
      c.Add(new AuthOperationHandler(authorizeAttribute));
    }
  };
}

That code is based on: http://haacked.com/archive/2011/10/19/implementing-an-authorization-attribute-for-wcf-web-api.aspx. This is no longer possible as MessageHandlers on the HttpConfiguration is not settable.

To summarize, my question is how can I specify certain message-handlers to only apply to certain ApiController services instead of all of them. It seems that ASP.NET MVC 4 WebApi framework has over simplified the power and configurability of the Web Api Beta.

1
  • If you self host, you can create multiple services. At the moment there is no way in Web host. Commented Mar 29, 2012 at 2:59

1 Answer 1

0

The recomended way to achieve this in the new Web API is with action filter attributes. They work pretty much the same way as in MVC, though you use a new set of base classes to implement them. The easiest way to get started is to derive from ActionFilterAttribute.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah. I am familiar with the actionfilters but in one of my cases I had a messagehandler which also called a ContinueWith so the flow was the message handler was called, then the actual service, then the message handler. The cool thing about that was I was able to pass variables from the entry message handler function to the exit message handler function. This is not possible with OnActionExecuting and OnActionExecuted as you can't pass variables from the Executing to the Executed functions.
Looks like ActionArguments could be a solution for that.
@nextgenneo ActionArguments are the parameters that will be passed into the action method. Don't use that. Instead, use HttpRequestMessage.Properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.