简体   繁体   English

生成的WEB API帮助区文档不显示响应正文格式/示例

[英]Generated WEB API Help Area documentation does not show response body formats/samples

I have created a help area documentation fro my web api 2 projects (based on owin/katana). 我已经为我的web api 2项目创建了一个帮助区域文档(基于owin / katana)。 I turned on everything setting in the config and installed Microsoft.AspNet.WebApi.OData . 我打开配置中的所有设置并安装了Microsoft.AspNet.WebApi.OData Currently I have the following settings: 目前我有以下设置:

config.SetSampleObjects(new Dictionary<Type, object>
{
    {typeof(string), "sample string"},
    {typeof(IEnumerable<string>), new string[]{"sample 1", "sample 2"}}
});

config.SetSampleForMediaType(
    new TextSample("Binary JSON content. See http://bsonspec.org for details."),
    new MediaTypeHeaderValue("application/bson"));

config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));

config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");

config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");

config.SetActualRequestType(typeof(string), "Values", "Get");

config.SetActualResponseType(typeof(string), "Values", "Post");

However, I don't any response sample being generated. 但是,我没有生成任何响应样本。 My page supposed to look like this as I saw on the web 我的网页看起来像我在网上看到的那样

应该是什么样子

but it it loos like this. 但它就像这样松散。 How do I get to display sample response formats like JSON as shown in the first picture ? 如何显示JSON等样本响应格式,如第一张图片所示?

在此输入图像描述

Try to decorate your controller action with the ResponseType attribute, like this: 尝试使用ResponseType属性修饰控制器操作,如下所示:

    [HttpGet]
    [Route("enterprise/")]
    [ResponseType(typeof(EnterpriseViewModel))]
    public IHttpActionResult Get()
    {
        ...
    }

If you have a constructor with paramaeters, you also must provide a blank constructor into your ViewModel class. 如果你有一个带有参数的构造函数,你还必须在ViewModel类中提供一个空的构造函数。

That's because of the content negotiation. 这是因为内容协商。 You're probably specifying something like this in your response: 您可能在回复中指定了类似的内容:

config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>))

Which prevents the content negotiation to work correctly and therefore, no JSON format have been generated in the help page. 这会阻止内容协商正常工作,因此,帮助页面中未生成任何JSON格式。

More here: http://blogs.msdn.com/b/yaohuang1/archive/2012/10/13/asp-net-web-api-help-page-part-2-providing-custom-samples-on-the-help-page.aspx 更多信息: http//blogs.msdn.com/b/yaohuang1/archive/2012/10/13/asp-net-web-api-help-page-part-2-providing-custom-samples-on-the -help-page.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM