Equivalent of [Bind(Prefix = "principalId")] in MVC4 Web Api?

asp.net-mvc, asp.net-mvc-routing, asp.net-web-api

Solution

You can use the `System.Web.Http.FromUriAttribute` attribute to specify the parameter names used for model binding.

public ActionResult ActionName(
                    [FromUri(Name = "principalID.userID")] int userID,
                    [FromUri(Name= "dependentID.applicationID")] long applicationID
                    )

`FromUri` tells model binding to examine the query string and the `RouteData` for the request.

Problem

Background: In MVC3, I've used the following syntax to specify custom `Action` parameter names: ``` public ActionResult ActionName([Bind(Prefix = "principalID")] int userID, [Bind(Prefix = "dependentID")] long applicationID) ``` The route for this action was defined as follows (`ActionNameConstraint` is a custom `IRouteConstraint`): ``` routes.MapHttpRoute( "DependantAction", "{controller}/{principalID}/{action}/{dependentID}", new {controller = @"[^0-9]+", action = ActionNameConstraint.Instance, dependentID = RouteParameter.Optional} ); ``` Question: The `BindAttribute` is a `System.Web.Mvc` class. Is there an equivalent of this (parameter binding) in Web Api? Of course, if there are other solutions to achieve the same result, I'd love to hear them!

Original source

Related problems