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

Source: Internet
Author: User

This error is encountered when ASP. NET MVC WebAPI generates a Help document when the parameters of the API are custom types.

Api. The following code comment can be removed from the Areas.HelpPage.HelpPageConfig Register method to eliminate the error.

Uncomment the following to use "[0]=foo&[1]=bar] directly as the sample for all actions, the support form URL ENC Oded format////and has ienumerable<string> as the            body parameter or return type.            Config. Setsamplefortype ("[0]=foo&[1]=bar", New Mediatypeheadervalue ("application/x-www-form-urlencoded"), typeof ( ienumerable<string>));

However, for different custom types, if you return the "[0]=foo&[1]=bar" string in the Help document, it is obvious that you cannot list the parameters of the post, and you cannot customize it based on the property type of the custom parameter.

The workaround is as follows:

    1. Create a new base class
 Public classParameter { Public  voidReg (httpconfiguration config) {Assembly _assembyle= assembly.getassembly ( This.             GetType ()); List<Type> Supportedclass = _assembyle.gettypes (). Where (t = t.basetype! =NULL&& T.basetype.name = ="Parameter").             ToList (); foreach(varSinchSupportedclass) {                 stringstr =""; Propertyinfo[] PS=s.getproperties (); foreach(varPinchPS) {                     Switch(p.propertytype.name) { Case "Guid": Str+ = P.name +"="+Guid.NewGuid ().                             ToString ();  Break;  Case "int":                          Case "Long": Str+ = P.name +"=1000"  ;  Break; default: Str+ = P.name +"="+P.name;  Break; } STR+="&"; } STR=str. TrimEnd ('&'); Config. Setsamplefortype (str,NewMediatypeheadervalue ("application/x-www-form-urlencoded"), s); }                                   }

2. Post's parameter class inherits base class

 Public classTestresultcomponentversion_body:parameter {/// <summary>        ///Testresultid/// </summary>         PublicGuid Testresultid {Get;Set; } /// <summary>        ///componentname/// </summary>         Public stringcomponentname {Get;Set; } /// <summary>        ///componentversion/// </summary>         Public stringcomponentversion {Get;Set; } }

3. API. The Reg method is called in Areas.HelpPage.HelpPageConfig's Register method

New Parameter (). Reg (config);

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.