Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core Swagger在ASP.CORE 3中为字典生成错误的URL_Asp.net Core_Swagger_Asp.net Core Webapi_Swashbuckle_Swashbuckle.aspnetcore - Fatal编程技术网

Asp.net core Swagger在ASP.CORE 3中为字典生成错误的URL

Asp.net core Swagger在ASP.CORE 3中为字典生成错误的URL,asp.net-core,swagger,asp.net-core-webapi,swashbuckle,swashbuckle.aspnetcore,Asp.net Core,Swagger,Asp.net Core Webapi,Swashbuckle,Swashbuckle.aspnetcore,当从查询字符串提取的模型将字典作为其属性之一时,Swagger生成错误的URL。如何告诉Swagger更改URL中字典的格式或手动定义输入参数模式,而不自动生成?试图使用虚张声势的皮带扣和NSwag 控制器 public class RecordsController : ControllerBase { [HttpGet] [Route("services/records")] public async Task<IActionResult> Records([From

当从查询字符串提取的模型将字典作为其属性之一时,Swagger生成错误的URL。如何告诉Swagger更改URL中字典的格式或手动定义输入参数模式,而不自动生成?试图使用虚张声势的皮带扣和NSwag

控制器

public class RecordsController : ControllerBase
{
  [HttpGet]
  [Route("services/records")]
  public async Task<IActionResult> Records([FromQuery] QueryModel queryModel)
  {
    return null;
  }
}
public class QueryModel 
{
  public int Page { get; set; }
  public int Count { get; set; }
  public Dictionary<Columns, string> Conditions { get; set; }
}
大摇大摆生成的URL-开放式API v2-将不绑定到“条件”

大摇大摆生成的URL-开放API v3-将不绑定到“条件”

自定义URL-按预期工作,并使用
{“UserId”,“1”}

问题

public class RecordsController : ControllerBase
{
  [HttpGet]
  [Route("services/records")]
  public async Task<IActionResult> Records([FromQuery] QueryModel queryModel)
  {
    return null;
  }
}
public class QueryModel 
{
  public int Page { get; set; }
  public int Count { get; set; }
  public Dictionary<Columns, string> Conditions { get; set; }
}
如何强制Swagger为Dictionary类型的属性呈现URL,如
PropertyName[Key]=Value

备选问题

这不是一个解决方案,但如果我以这种方式定义输入参数的默认值,Swagger将创建正确的URL

{
  "Conditions[UserId]": "1",
  "Conditions[GroupId]": "2"
}
URL现在是正确的,并且已正确绑定到模型

/services/records?Page=0&Count=5&Conditions[UserId]=1&Conditions[GroupId]=2 

有没有办法更改Swagger中显示的字典输入类型的默认值?

您需要为查询定义设置查询样式
deepObject

NSwag目前支持这一点,您将通过它设置值
deepObject

我也很好奇如何在没有NSwag的情况下做到这一点,所以我看了一下

在这里,您可以为它提供您的静态json招摇过市,如果您希望看到创建相同设置的不同方式,它将为您生成一个服务器

字典的示例模型

[DataContract]
public partial class Dictionary : IEquatable<Dictionary>
{ 
    /// <summary>
    /// Gets or Sets Word
    /// </summary>
    [DataMember(Name="word")]
    public string Word { get; set; }

    /// <summary>
    /// Gets or Sets Define
    /// </summary>
    [DataMember(Name="define")]
    public string Define { get; set; }

查看我尝试过的

中的deepObject样式,然后大摇大摆地呈现URL,如/services/records?Page=0&Count=5&Conditions.UserId=1&Conditions.GroupId=2而不是Conditions[UserId]=1&Conditions[GroupId]=2,在本例中,我只能传递指定的参数,如“UserId”和“GroupId”,没有任何数量的字典条目。当你说你尝试了,你从上面的例子中尝试了哪一部分。这样我就可以帮助你缩小为什么它不适合你
/services/records?Page=0&Count=5&Conditions[UserId]=1&Conditions[GroupId]=2 
[DataContract]
public partial class Dictionary : IEquatable<Dictionary>
{ 
    /// <summary>
    /// Gets or Sets Word
    /// </summary>
    [DataMember(Name="word")]
    public string Word { get; set; }

    /// <summary>
    /// Gets or Sets Define
    /// </summary>
    [DataMember(Name="define")]
    public string Define { get; set; }
    /// <summary>
    /// Get word definition
    /// </summary>
    /// <remarks>Get me the word definitions</remarks>
    /// <param name="dictionary">Status values that need to be considered for filter</param>
    /// <response code="200">successful operation</response>
    [HttpGet]
    [Route("/v2/book")]
    [ValidateModelState]
    [SwaggerOperation("BookGet")]
    public virtual IActionResult BookGet([FromQuery][Required()]Dictionary dictionary)
/book:
  get:
    summary: Get word definition
    description: Get me the word definitions
    parameters:
    - name: dictionary
      in: query
      description: Status values that need to be considered for filter
      required: true
      style: deepObject
      schema:
        type: object
        properties:
          word: 
            type: string
          define:
            type: string