[英]How to display parameter in Web API help page documentation for Response only?
[英]Generated WEB API Help Area documentation does not show response body formats/samples
我已經為我的web api 2項目創建了一個幫助區域文檔(基於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");
但是,我沒有生成任何響應樣本。 我的網頁看起來像我在網上看到的那樣
但它就像這樣松散。 如何顯示JSON等樣本響應格式,如第一張圖片所示?
嘗試使用ResponseType屬性修飾控制器操作,如下所示:
[HttpGet]
[Route("enterprise/")]
[ResponseType(typeof(EnterpriseViewModel))]
public IHttpActionResult Get()
{
...
}
如果你有一個帶有參數的構造函數,你還必須在ViewModel類中提供一個空的構造函數。
這是因為內容協商。 您可能在回復中指定了類似的內容:
config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>))
這會阻止內容協商正常工作,因此,幫助頁面中未生成任何JSON格式。
更多信息: 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.