C# 为实体参数生成描述字段

C# 为实体参数生成描述字段,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=

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


您可以添加描述属性:

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

发信人:

好吧,我找到了答案,希望这能帮助其他遇到这个问题的人。您要做的第一件事是按照此步骤为ApiExplorer启用XML文档。启用要添加的内容后

/// <summary>Description</summary>
///说明
在控制器名称上方(您也可以通过添加另一行测试模型来在xml中添加参数名称)

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

public class TestModel()
{
/// <summary>This is your IdNumber you received earlier</summary>
public string IdNumber {get;set;}
}
公共类TestModel()
{
///这是您之前收到的ID号码
公共字符串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/Products/5
公共字符串Get(int-id)
{
返回“值”;
}
如果运行应用程序并转到api帮助页面,则文档应可见

  • 右键单击项目,单击属性->构建->单击XML构建,如图所示
  • 转到项目->应用程序启动->HelpPageConfig.cs中的区域文件夹

  • 如果有注释,请取消注释下面的行

    config.SetDocumentationProvider(新的XmlDocumentationProvider(HttpContext.Current.Server.MapPath(“~/bin/projectname.xml”))

  • 将MapPath文件名更改为“生成XML文档文件”列中给定的文件名*projectname将更改为您的项目名称


  • 不幸的是,这不起作用。在主帮助页面上,我仍然留有空白来描述主体参数