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

C# 生成的WEB API帮助区域文档不显示响应正文格式/示例,c#,asp.net-web-api,documentation,asp.net-web-api2,asp.net-mvc-areas,C#,Asp.net Web Api,Documentation,Asp.net Web Api2,Asp.net Mvc Areas,我已经为我的WebAPI2项目(基于owin/katana)创建了一个帮助区域文档。我打开了配置中的所有设置,并安装了Microsoft.AspNet.WebApi.OData。目前我有以下设置: config.SetSampleObjects(new Dictionary<Type, object> { {typeof(string), "sample string"}, {typeof(IEnumerable<string>), new string[

我已经为我的WebAPI2项目(基于owin/katana)创建了一个帮助区域文档。我打开了配置中的所有设置,并安装了
Microsoft.AspNet.WebApi.OData
。目前我有以下设置:

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");
config.SetSampleObjects(新字典
{
{typeof(string),“sample string”},
{typeof(IEnumerable),新字符串[]{“样本1”,“样本2”}
});
config.SetSampleForMediaType(
新建TextSample(“二进制JSON内容。请参阅http://bsonspec.org 详情请参阅。”),
新MediaTypeHeaderValue(“应用程序/bson”);
config.SetSampleForType(“[0]=foo&[1]=bar”),新的MediaTypeHeaderValue(“application/x-www-form-urlencoded”),typeof(IEnumerable));
config.SetSampleRequest(“1234”、新MediaTypeHeaderValue(“文本/普通”)、“值”、“Put”);
config.SetSampleResponse(newImageSample(“../images/aspNetHome.png”)、newMediaTypeHeaderValue(“image/png”)、“Values”、“Get”、“id”);
SetActualRequestType(typeof(string),“Values”,“Get”);
SetActualResponseType(typeof(string),“Values”,“Post”);
但是,我没有生成任何响应样本。我的网页应该像我在网上看到的那样

但事情是这样的。如何显示如第一张图片所示的JSON等示例响应格式


这是因为内容协商。 您可能在响应中指定了类似的内容:

config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>))
config.SetSampleForType(“[0]=foo&[1]=bar”),新的MediaTypeHeaderValue(“application/x-www-form-urlencoded”),typeof(IEnumerable))
这会阻止内容协商正常工作,因此,帮助页面中没有生成JSON格式


更多信息:

尝试用ResponseType属性修饰控制器操作,如下所示:

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

如果有带参数的构造函数,还必须在ViewModel类中提供空白构造函数

空白构造函数!塔克斯!