我正在努力让Swagger正确显示我的ServiceStack服务。
我希望将UserId字符串作为表单参数,将PrivateCustomer对象作为body参数,但是尽管UserId也作为单独的输入字段出现,但仍要同时使用UserId和PrivateCustomer来获取body参数。
如何摆脱体内的UserId?
非常感谢!
答案 0 :(得分:1)
[Api*]
批注仅用于记录您的API,它们不会影响您API的行为,例如始终希望请求主体是整个请求DTO,并且不能同时发送“ form”和“ body”参数类型,即,只有1个请求主体,而当使用“ form”时,仅表单变量将被发送。
如果要分隔它们,可以使用以下命令将UserId
添加到查询字符串中,并使其不出现在模型架构中:
[Route("/CreatePrivateCustomer", "POST")]
public class CreatePrivateCustomerRequest
{
[ApiMember(IsRequired = true, ParameterType = "query", ExcludeInSchema = true)]
public string UserId { get; set; }
[ApiMember(IsRequired = true, ParameterType = "model")]
public PrivateCustomer Customer { get; set; }
}
这将分隔变量并在queryString中发送UserId
,并在请求正文中将DTO的请求作为JSON发送,例如:
POST /CreatePrivateCustomer?UserId=1
Content-Type: application/json
{"Customer":{"CustomerNumber":1,...}}
尽管通常来说,如果您希望将所需的参数与请求正文分开,则可以将它们放在路径中,例如:
[Route("/CreatePrivateCustomer/{UserId}", "POST")]
public class CreatePrivateCustomerRequest
{
[ApiMember(IsRequired = true, ParameterType = "path", ExcludeInSchema = true)]
public string UserId { get; set; }
[ApiMember(IsRequired = true, ParameterType = "model")]
public PrivateCustomer Customer { get; set; }
}
,如果您不希望嵌套PrivateCustomer
属性,则可以将它们直接添加到“请求DTO”中,例如:
[Route("/CreatePrivateCustomer/{UserId}", "POST")]
public class CreatePrivateCustomerRequest
{
[ApiMember(IsRequired = true, ParameterType = "path", ExcludeInSchema = true)]
public string UserId { get; set; }
public int CustomerNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}