【发布时间】:2026-01-31 04:00:01
【问题描述】:
我正在编写一个基于 .NET Framework 4.7.1 的 ASP.NET Core 服务。该服务旨在使用 Swagger 实现 REST API,我需要(至少一开始)能够在 LINQPad 中运行它。
我使用 NSwagStudio 从示例 petstore yaml 生成控制器。但是,我正在努力弄清楚如何将我的控制器与所有 ASP.NET Core 胶水代码结合起来。我在网上找到了在 .NET Core 中执行此操作的说明,但在 .NET Framework 中似乎需要以不同的方式完成。
非常感谢您在连接点方面的帮助。我的示例代码运行但导航到 SwaggerUI 给我一条消息“规范中没有定义操作”。我认为这是因为控制器没有被拾起,但我无法弄清楚丢失了什么!
参考资料:
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference>
<Reference><RuntimeDirectory>\System.Runtime.Serialization.dll</Reference>
<NuGetReference>Microsoft.AspNetCore</NuGetReference>
<NuGetReference>Microsoft.AspNetCore.Hosting</NuGetReference>
<NuGetReference>Microsoft.AspNetCore.Mvc</NuGetReference>
<NuGetReference>Microsoft.AspNetCore.Mvc.WebApiCompatShim</NuGetReference>
<NuGetReference>Microsoft.AspNetCore.StaticFiles</NuGetReference>
<NuGetReference>Swashbuckle.AspNetCore</NuGetReference>
<Namespace>Microsoft.AspNetCore</Namespace>
<Namespace>Microsoft.AspNetCore.Builder</Namespace>
<Namespace>Microsoft.AspNetCore.Hosting</Namespace>
<Namespace>Microsoft.AspNetCore.Http</Namespace>
<Namespace>Microsoft.Extensions.DependencyInjection</Namespace>
<Namespace>Microsoft.OpenApi.Models</Namespace>
<Namespace>System.Web.Http</Namespace>
控制器:
[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.9.4.0 (NJsonSchema v10.3.1.0 (Newtonsoft.Json v11.0.0.0))")]
[Microsoft.AspNetCore.Mvc.Route("petstore.swagger.io/v2")]
public partial class Controller : Microsoft.AspNetCore.Mvc.ApiController
{
private IController _implementation;
public Controller(IController implementation)
{
_implementation = implementation;
}
/// <summary>Add a new pet to the store</summary>
/// <param name="accept_Language">The language you prefer for messages. Supported values are en-AU, en-CA, en-GB, en-US</param>
/// <param name="cookieParam">Some cookie</param>
[Microsoft.AspNetCore.Mvc.HttpPost, Microsoft.AspNetCore.Mvc.Route("pet")]
public System.Threading.Tasks.Task AddPet([Microsoft.AspNetCore.Mvc.FromBody] object body, [Microsoft.AspNetCore.Mvc.FromHeader(Name = "Accept-Language")] string accept_Language, long cookieParam)
{
return _implementation.AddPetAsync(body, accept_Language ?? "en-AU", cookieParam);
}
/// ...
}
主要:
void Main()
{
WebHost
.CreateDefaultBuilder()
.UseStartup<Startup>()
.UseUrls("http://localhost:5000/")
.Build()
.Run();
}
启动:
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Test API", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.DocumentTitle = "Test Swagger Interface";
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Test V1");
});
}
}
【问题讨论】:
-
为什么需要它在 linqPad 中运行? .Net-core 足够通用,可以托管自己,IDK 如果 linqPad 可以支持托管应用程序
-
这最终将成为更大的 Windows 服务的一部分,该服务侦听跨不同类型中间件的请求。不过,目前在 LINQPad 中运行它会更容易测试。我不明白为什么 LINQPad 不能托管它。毕竟,SwaggerUI 加载正确,如果我删除 Swagger,我可以定义一个从主页返回“Hello world”的 MVC 路由。
-
嗯,有趣的是,我从来没有从 LINQPad 托管过,我一直发现单独运行应用程序很容易,但如果它有助于测试你的能力。
标签: c# .net asp.net-core swagger linqpad