简体   繁体   English

如何在Swashbuckle中处理路径/路由参数?

[英]How do I handle path/route parameters in Swashbuckle?

I have a controller where I get the ID from the route. 我有一个控制器,可以从路由中获取ID。

[HttpGet]
[Route("{vehicleId}")]
public InfoDto GetInfo([FromUri] VehicleDetailsRequest request)
{
   return ...;
}

The VehicleDetailsRequest object looks like this (the Validator is from FluentValidation): VehicleDetailsRequest对象如下所示(验证器来自FluentValidation):

[Validator(typeof(VehicleDetailsRequestValidator))]
public class VehicleDetailsRequest
{
    public int VehicleId { get; set; }

    public string Lang { get; set; }
}

I can query this action as I expect with http://localhost/controller/123?lang=sv but my swagger documentation looks like this: 我可以使用http://localhost/controller/123?lang=sv查询此操作,但是我的招摇文档如下所示:

带有重复参数的Swagger文档

How can I get Swashbuckle/Swagger to only show me one vehicleId but still keep the FluentValidation? 我怎样才能得到Swashbuckle /扬鞭只告诉我一个vehicleId但仍保持FluentValidation?

I'm using Swashbuckle 5.6 and .Net Framework 4.6.2. 我正在使用Swashbuckle 5.6和.Net Framework 4.6.2。

In my opinion, there is something wrong with action method signature. 我认为操作方法签名存在问题。 The parameter from the URL should be present in the action method parameters. URL的参数应存在于操作方法参数中。

Also, if you want only lang in query string, then it should also be present as a parameter in the action. 另外,如果您只想在查询字符串中使用lang,那么它也应该在操作中作为参数出现。

Please note that if you specify object in the action parameters, all its properties would be forming the query string. 请注意,如果在操作参数中指定对象,则其所有属性都将构成查询字符串。

So, your action method should like below: 因此,您的操作方法应如下所示:

[HttpGet]
[Route("{vehicleId}")]
public InfoDto GetInfo(int vehicleId, [FromUri] string lang)
{
   return ...;
}

This should help you to resolve the issue. 这应该可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM