我正在尝试为通话创建一个示例
public HttpResponseMessage Post([FromBody]JObject value)按照此处描述的示例ASP.NET Web API帮助页面第2部分:在帮助页面上提供自定义示例我设置文件“HelpPageConfig.cs”
config.SetSampleResponse("test", new MediaTypeHeaderValue("application/json"), "Eventos", "Post", "value");我的控件被命名为“EventosController.cs”,只有在访问示例中的页面时显示“not available sample”
我要展示的示例如下:
{ "field1":"value1", "field2":"value2", "field3":"value3", "field4":"value4", "field5":"value5", "field6":"value6", "field7":"value7", }还有什么需要做的?
I'm trying to create a sample for a call
public HttpResponseMessage Post([FromBody]JObject value)following the example described here ASP.NET Web API Help Page Part 2: Providing custom samples on the Help Page I setup the file "HelpPageConfig.cs"
config.SetSampleResponse("test", new MediaTypeHeaderValue("application/json"), "Eventos", "Post", "value");my control is named "EventosController.cs", only when accessing the page in sample shows "not available sample"
the sample I want to show looks like:
{ "field1":"value1", "field2":"value2", "field3":"value3", "field4":"value4", "field5":"value5", "field6":"value6", "field7":"value7", }what remains to be done?
最满意答案
您的问题是您正在显示示例响应 ,而不是请求 。 对于完整的帮助页面,您可能需要添加一个示例请求(对于此控制器和操作,或者如果您打算对类型使用相同的示例 - 则为类型)
您的HelpPageConfig中有类似的内容:
public static void Register(HttpConfiguration config) { var requestSample = @"{ field1: 'value1', field2: 'value2' }"; //if there would be just 1 sample for all JObject types: config.SetSampleForType( requestSample, new MediaTypeHeaderValue("application/json"), typeof(JObject)); //if you want to write the specific sample for each method/media type config.SetSampleRequest(requestSample, new MediaTypeHeaderValue("text/json"), "Eventos", "Post"); config.SetSampleRequest(requestSample, new MediaTypeHeaderValue("application/x-www-form-urlencoded"), "Eventos", "Post"); config.SetSampleRequest(requestSample, new MediaTypeHeaderValue("application/json"), "Eventos", "Post"); //and finally, response sample var responseSample = "my response sample response"; config.SetSampleResponse(responseSample, new MediaTypeHeaderValue("application/x-www-form-urlencoded"), "Eventos", "Post", "value"); }Your problem is that you are showing the sample response, not request. For a complete help page you might want to add a sample request (for this controller and action or if you plan to use the same sample for type - then for type)
Something like this in your HelpPageConfig:
public static void Register(HttpConfiguration config) { var requestSample = @"{ field1: 'value1', field2: 'value2' }"; //if there would be just 1 sample for all JObject types: config.SetSampleForType( requestSample, new MediaTypeHeaderValue("application/json"), typeof(JObject)); //if you want to write the specific sample for each method/media type config.SetSampleRequest(requestSample, new MediaTypeHeaderValue("text/json"), "Eventos", "Post"); config.SetSampleRequest(requestSample, new MediaTypeHeaderValue("application/x-www-form-urlencoded"), "Eventos", "Post"); config.SetSampleRequest(requestSample, new MediaTypeHeaderValue("application/json"), "Eventos", "Post"); //and finally, response sample var responseSample = "my response sample response"; config.SetSampleResponse(responseSample, new MediaTypeHeaderValue("application/x-www-form-urlencoded"), "Eventos", "Post", "value"); }更多推荐
发布评论