【问题标题】:Generate Description Fields for Body Parameters为主体参数生成描述字段
【发布时间】:2015-09-01 19:02:31
【问题描述】:

所以我一直在谷歌上搜索很多东西来尝试解决这个问题,但我似乎找不到任何东西。请参阅图片以供参考,但我正在尝试填写主体参数的描述字段。最好的方法是什么?

【问题讨论】:

标签: c# asp.net asp.net-mvc-apiexplorer


【解决方案1】:

您可以添加描述属性:

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

发件人:http://millionbonus.bitbucket.org/mvc.apiexplorer/

【讨论】:

  • 不幸的是,这不起作用。我仍然在主体参数的描述和主帮助页面上留有空白
【解决方案2】:

好的,所以我想通了,希望这可以帮助遇到此问题的其他人。您要做的第一件事是按照link 为 ApiExplorer 启用 XML 文档。启用后要添加

/// <summary>Description</summary>

在控制器名称上方(您也可以通过添加另一行 &lt;param name="model"&gt;A Test Model&lt;/param&gt; 在 xml 中添加参数名称)

然后转到您的模型,并为模型中的每个参数再次添加一个摘要标签,如下所示:

public class TestModel()
{
/// <summary>This is your IdNumber you received earlier</summary>
public string IdNumber {get;set;}
}

【讨论】:

    【解决方案3】:

    我发现这里的答案令人困惑,所以这是我的完整解决方案。

    首先打开 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 帮助页面,文档应该是可见的。

    【讨论】:

      【解决方案4】:
      1. 右击项目,点击属性->构建->点击如图所示的XML构建。

      1. 进入项目中的Areas文件夹 -> App_Start -> HelpPageConfig.cs

      2. 如果有注释,请取消注释以下行。

        config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/bin/projectname.xml")));

      3. 将 MapPath 文件名更改为构建 XML 文档文件列中给出的名称。 *projectname 将更改为您的项目名称。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-15
        • 1970-01-01
        • 1970-01-01
        • 2021-10-07
        • 1970-01-01
        • 2015-12-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多