WEB API使用复杂参数“jobject”创建示例方法(WEB API Create sample method with complex parameter “jobject”)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
WEB API使用复杂参数“jobject”创建示例方法(WEB API Create sample method with complex parameter “jobject”)

我正在尝试为通话创建一个示例

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"); }

更多推荐

本文发布于:2023-04-20 18:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/ae892a0d60cb5346004a66c0b9c36f4e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   参数   方法   jobject   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!