A lot of our Web API action patterns have the following pattern:
public IHttpActionResult GetDetails(Guid customerGuid)
{
Customer customer = customerRetriever.FromGuid(customerGuid);
if (customer == null)
return NotFound();
// Do Stuff....
}
I want to move all the customer lookup boiler plate out of the action method. I created an action filter attribute to do this, by looking at the value of the action argument and setting a request property:
KeyValuePair<String, Object> argument = actionContext.ActionArguments.Single(kvp => kvp.Key.Equals(ParameterName));
Guid customerGuid = (Guid)argument.Value;
// Do customer lookup, return 404 if needed
// Add customer to request properties
This works, but i still need to include the now redundant Guid in my action signature. If i remove this, my approach for finding the Guid value fails. Is there a way to bind a value from the URI, without having that value as an action parameter?