首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带参数的NSwagStudio反射行为

带参数的NSwagStudio反射行为
EN

Stack Overflow用户
提问于 2021-02-13 11:53:45
回答 1查看 578关注 0票数 0

我创建了一个简单的.net5 web。然后我得到伟大的Rico的Nswag.aspnetcore,并调整它以获得我的应用程序来创建swagger.json文件

在脚手架控制器中,我编写了另一个PUT操作,它接受一个对象参数(最简单)

代码语言:javascript
复制
[HttpPost]
public ActionResult simple(WeatherForecast w)
{
    return NoContent();
}

一切都很顺利。我可以在http://localhost:63630/swagger/v1/swagger.json中找到这个文件。

然后,我将使用NSwagStudio获得具有接口的C#客户端代理。

使用OpenAPI规范,我将获得一个漂亮的c#接口:

代码语言:javascript
复制
[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.10.2.0 (NJsonSchema v10.3.4.0 (Newtonsoft.Json v12.0.0.0))")]
public partial interface IWeatherForecastClient
{
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync();

    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync(System.Threading.CancellationToken cancellationToken);

    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<FileResponse> SimpleAsync(WeatherForecast w);

    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<FileResponse> SimpleAsync(WeatherForecast w, System.Threading.CancellationToken cancellationToken);

}

现在,我将使用NSwagStudio选项"WebAPI反射“,Config:

用于引用程序集文件的.\bin\Debug\net5.0\WebApplication7.dll

  • Path

  • .Net程序集路径=>

  • 程序集(
  • .Net => =>)=>
  • 程序集使用ASP.NET核心

接口是

代码语言:javascript
复制
[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.10.2.0 (NJsonSchema v10.3.4.0 (Newtonsoft.Json v12.0.0.0))")]
public partial interface IWeatherForecastClient
{
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync();

    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync(System.Threading.CancellationToken cancellationToken);

    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<FileResponse> SimpleAsync(System.DateTimeOffset? date, int? temperatureC, int? temperatureF, string summary);

    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<FileResponse> SimpleAsync(System.DateTimeOffset? date, int? temperatureC, int? temperatureF, string summary, System.Threading.CancellationToken cancellationToken);

}

post方法中的参数已被取消构造!

问题是:如何使用反射来获得与OpenAPI规范选项相同的接口?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-17 16:57:30

不要在WebAPI核心中使用“ASP.NET反射”,因为它只适用于遗留的ASP.NET (非核心!)。使用"ASP.NET核心通过assemblies“,这也或多或少是基于反射的(加载程序集或.csproj)。我建议使用.csproj作为输入而不是DLL.如果DI中没有注册的NSwag文档,也可以通过UI指定配置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66184806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档