Lucio Paiva Lucio Paiva - 10 months ago 41
ASP.NET (C#) Question

ValueProvider never being called

I'm working with MVC 4 Web API and I have this dummy ValueProvider:


class DummyValueProvider : IValueProvider
public DummyValueProvider()

public bool ContainsPrefix(string prefix)
return true;

public ValueProviderResult GetValue(string key)
return new ValueProviderResult("testing", "testing", System.Globalization.CultureInfo.InvariantCulture);

class DummyValueProviderFactory : System.Web.Http.ValueProviders.ValueProviderFactory
public override IValueProvider GetValueProvider(System.Web.Http.Controllers.HttpActionContext actionContext)
return new DummyValueProvider();

This ValueProvider should return
for any key asked, so it will always supply a value to the model binder when it needs. The ValueProvider is registered in the WebApiConfig like this:


config.Services.Add(typeof(ValueProviderFactory), new DummyValueProviderFactory());

The code compiles and runs fine.
I also have this action in the Account API controller:


public HttpResponseMessage Register(string foo) { ... }

The action gets called fine when I call it like below:


is filled with
as expected; but if I call:


The server returns 404 with the message
No HTTP resource was found that matches the request URI 'http://localhost:14459/register'

Also, I put breakpoints inside methods ContainsPrefix() and GetValue(), but they never get triggered.

What am I doing wrong? Shouldn't DummyValueProvider be providing the value
to parameter

Answer Source

Try this

public HttpResponseMessage Get([ValueProvider(typeof(DummyValueProviderFactory))] string foo) {... }

I higly suggest you to read this recent article to customize Web Api Binding.

After reading the article the OP was able to discover the solution. It was that using the parameter attribute [ModelBinder] was required for it to work. This was because unless the parameter is annotated, [FromUri] is assumed. Once annotated with [ModelBinder] the registered handlers are executed.