Web API, model binding, [FromUri], and WebApiExplorer not playing well together

1.7k Views Asked by At

I have a fairly simple web api project and have the following method in a CustomerController:

public class CustomerController : ApiController
{
    [HttpGet]
    public UpdatePasswordResult UpdatePasswordWithUserCode([FromUri] UpdatePasswordWithUserCodeCriteria passwordChangeRequest)
    {
        ...
    }
}

This works fine for model binding, my UpdatePasswordWithUserCodeCriteria passwordChangeRequest variable gets populated when the user types appropriate values in the URL.

public class UpdatePasswordWithUserCodeCriteria : IParameterCriteria
{
    public string securityCode { get; set; }
    public string newPassword { get; set; }
}

However, my help docs (generated with ApiExplorer) never sees this complex type and don't document it. I found this area "Setting custom samples" on the following page: http://blogs.msdn.com/b/yaohuang1/archive/2012/10/13/asp-net-web-api-help-page-part-2-providing-custom-samples-on-the-help-page.aspx

In my HelpPageConfig.cs file I have:

config.SetSampleForType(
    "securityCode=asdfsadfsadf&newPassword=Foo",
    new MediaTypeHeaderValue("application/x-www-form-urlencoded"),
    typeof(UpdatePasswordWithUserCodeCriteria));

And this doesn't generate the documentation for the parameter unless I take out the [FromUri] attribute of the parameter. When I remove the [FromUri], the ApiExplorer correctly generates the help docs with the parameters, but then model binding doesn't work for the parameters:

public class CustomerController : ApiController
{
    [HttpGet]
    public UpdatePasswordResult UpdatePasswordWithUserCode(UpdatePasswordWithUserCodeCriteria passwordChangeRequest)
    {
        ...
    }
}

How can I get both model binding to work for this request AND web Api Explorer to generate the parameter documentation for it?

0

There are 0 best solutions below