Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude media-type sample from Web API help page

I am newbie in Web API usage, and the problem I'm struggling with problem I can't find solution to. Common problem is, when instead of application/x-www-form-urlencoded media type sample, people get

Failed to generate the sample for media type 'application/x-www-form-urlencoded'. Cannot use formatter 'FormUrlEncodedMediaTypeFormatterTracer' to write type 'Task'.

Common suggestion is to specify own sample in the Config file, but I'm wondering, can I remove this media type help section?

config.SetSampleForType("", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(object));

Gives me an empty section.

Thanks in advance!

like image 624
insomnium_ Avatar asked Aug 15 '13 08:08

insomnium_


2 Answers

  • May I ask as to why you would want to remove this section? Like you don't want to support formurlencoded formatter in your service? if yes, then you can remove the formatter itself from the collection of formatters, in which case this section wouldn't show up.

    config.Formatters.Clear();
    config.Formatters.Add(new JsonMediaTypeFormatter());
    config.Formatters.Add(new XmlMediaTypeFormatter());  
    
  • Alternatively, you can do the following change (highlighed comments) in the file at Areas\HelpPage\SampleGeneration\HelpPageSampleGenerator.cs and do the following, which is to filter out the formatters:

        // Do the sample generation based on formatters only if an action doesn't return an HttpResponseMessage.
        // Here we cannot rely on formatters because we don't know what's in the HttpResponseMessage, it might not even use formatters.
        if (type != null && !typeof(HttpResponseMessage).IsAssignableFrom(type))
        {
            object sampleObject = GetSampleObject(type);
    
            // Change Begin --------------------------------------
            IEnumerable<MediaTypeFormatter> filteredFormatters = formatters.Where(frmtr => frmtr.GetType() != typeof(JQueryMvcFormUrlEncodedFormatter));
    
            foreach (var formatter in filteredFormatters)
            {
    
            // Change End --------------------------------------
    
like image 156
Kiran Avatar answered Oct 17 '22 01:10

Kiran


If you want to remove specific format type use this code:

            var matches = config.Formatters
                            .Where(f => f.SupportedMediaTypes
                                         .Where(m => m.MediaType.ToString() == "application/xml" ||
                                                     m.MediaType.ToString() == "text/xml" ||
                                                     m.MediaType.ToString() == "application/x-www-form-urlencoded")

                                         .Count() > 0)
                            .ToList();
        foreach (var match in matches)
            config.Formatters.Remove(match);
like image 23
Srinivasa Rao Avatar answered Oct 16 '22 23:10

Srinivasa Rao