1
Vote

Web Api - Populating action parameters through ActionFilterAttribute no longer working

description

Populating an Web Api action method parameter using an action filter no longer works in MVC 4 RTM.
Used to work in RC...

To duplicate the issue:
  • Create a default Web Api project
  • Add a POST method to ValuesController, as such
    [AuthorizationKeyFilterAttribute("applicationToken")]
    public void Post(Guid applicationToken)
    {
    }
  • Create the action filter attribute as
    [AttributeUsage(AttributeTargets.Method)]
    public sealed class AuthorizationKeyFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
    {
    public AuthorizationKeyFilterAttribute(string parameterName)
    {
    ParameterName = parameterName;
    }

    public string ParameterName { get; private set; }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
    var value = actionContext.Request.Headers.Authorization;
    actionContext.ActionArguments[ParameterName] = value;
    }
    }
Run with:
  • Url: http://localhost:8100/api/values/
  • Method: POST
  • Headers: Authorization: 7199e463-3a9b-4843-94d9-0dce9e728dbb
I get the following error:
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:8100/api/values/'.","MessageDetail":"No action was found on the controller 'Values' that matches the request."}

Any ideas how can I get this feature working again ?

comments

kichalla wrote Sep 5, 2012 at 7:58 PM

You could do this with HttpParameterBinding. In the following blog post, just like how the Thread.CurrentPrincipal is being used to binding to a parameter, you could get the request headers and bind it.

http://blogs.msdn.com/b/jmstall/archive/2012/05/11/webapi-parameter-binding-under-the-hood.aspx

robertberet wrote Sep 10, 2012 at 10:43 AM

Thanks,
works great.

HongmeiG wrote Apr 6 at 6:11 AM

Although the HttpParameterBinding would work, the original code should also work. Please investigate.