【发布时间】:2015-09-01 19:02:31
【问题描述】:
【问题讨论】:
标签: c# asp.net asp.net-mvc-apiexplorer
【问题讨论】:
标签: c# asp.net asp.net-mvc-apiexplorer
您可以添加描述属性:
[Description("Get the data from our service. It will requires a key.")]
public ActionResult GetData(string key)
{
//Do something here...
return Json(new{Success=true, Data = data});
}
或用于参数
public ActionResult GetData([Description("A valid key should be formated as xxx-xxx-xx")]string key)
{
//Do something here...
return Json(new{Success=true, Data = data});
}
【讨论】:
好的,所以我想通了,希望这可以帮助遇到此问题的其他人。您要做的第一件事是按照link 为 ApiExplorer 启用 XML 文档。启用后要添加
/// <summary>Description</summary>
在控制器名称上方(您也可以通过添加另一行 <param name="model">A Test Model</param> 在 xml 中添加参数名称)
然后转到您的模型,并为模型中的每个参数再次添加一个摘要标签,如下所示:
public class TestModel()
{
/// <summary>This is your IdNumber you received earlier</summary>
public string IdNumber {get;set;}
}
【讨论】:
我发现这里的答案令人困惑,所以这是我的完整解决方案。
首先打开 XMLDocumentation,方法是转到 Areas -> HelpPage -> App_Start -> HelpPageConfig.cs 并取消注释以下两行。
// Uncomment the following to use the documentation from XML documentation file.
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
然后对于您要提供文档的方法,以以下格式创建 xml 注释。这对我来说通常是自动完成的,但我打开了 resharper,所以这可能不是默认设置。
/// <summary>
/// An example method description
/// </summary>
/// <param name="id">An example parameter description</param>
/// <returns>An example return value description</returns>
// GET: api/Products/5
public string Get(int id)
{
return "value";
}
如果您运行应用程序并转到您的 api 帮助页面,文档应该是可见的。
【讨论】: