Failed to generate the sample for media type 'application/x-www-form-urlencoded'

The answer helped me out but i got bored of writing an example for each type that the system didn't know about... what i ended up doing is this

Type[] types = { typeof(MyType), typeof(AnotherType), *add more here* };

foreach(Type t in types)
{
    List<string> propExample = new List<string>();
    foreach(var p in t.GetProperties())
    {
        propExample.Add(p.Name + "=value");
    }

    config.SetSampleForType(string.Join("&", propExample), new MediaTypeHeaderValue("application/x-www-form-urlencoded"), t);            
}

Someone could get clever and extend it so that it puts in default values based on type of property but this was enough for my purposes.


This is an expected behavior. HelpPage sample generation uses the actual formatters present on the HttpConfiguration to 'write' the sample objects. FormUrlEncodedMediaTypeFormatter cannot 'write' any type, hence HelpPage cannot generate samples for it. As a workaround you could explicitly supply a sample for a particular type (as shown in the Areas\HelpPage\App_Start\HelpPageConfig.cs's commented code).

config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));