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: 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
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: 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