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:
- 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 '.