【发布时间】:2019-01-22 04:26:03
【问题描述】:
我以非标准方式编写 Web API 控制器,将参数作为动态对象获取。
这会导致 NSwag 出现问题。因为方法定义中没有参数,所以 NSwag 无法生成需要的东西。
我想知道在这种情况下是否有任何使用 NSwag 的选项。也许我可以在方法中添加一些属性,以便 NSwag 能够生成 API?
[HttpPost]
[ActionName("create-account")]
public IHttpActionResult CreateAccount()
{
var body = Request.Content.ReadAsStringAsync().Result;
dynamic json = Utils.GetJsonBody(body);
if (!Utils.GetJsonPropertyValueByPropertyName<String>(json, "email", out String email))
{
return Content(HttpStatusCode.BadRequest, "Please provide a valid email.".AsApiMessageResult());
}
if (!Utils.GetJsonPropertyValueByPropertyName<String>(json, "name", out String name))
{
return Content(HttpStatusCode.BadRequest, "Please provide an account name.".AsApiMessageResult());
}
if (!Utils.GetJsonPropertyValueByPropertyName<String>(json, "domain", out String domain))
{
return Content(HttpStatusCode.BadRequest, "Please provide a valid domain.".AsApiMessageResult());
}
【问题讨论】:
-
请停止编辑问题以删除相关语言标签并说您选择使用其他内容。即使您选择使用其他东西,您的问题仍然是有效的,并且您将事情转向不同的方向与这个问题无关。语言标签很有用,因为它提供语法高亮(在这种情况下直接相关)。
标签: c# asp.net-web-api nswag