当我将 Fastendpoint 与 Get 方法和查询字符串参数 swagger 文档一起使用时,它似乎没有正确创建,它只是显示为正文的一部分,如果我在正文中提供查询字符串值,这将导致错误,任何想法?
如果您能提供在带有 Request 对象和不带 Request 对象的情况下使用 Swagger 的示例代码,我们将不胜感激。
当我有一个请求对象时,它允许我放置请求对象的属性,但它失败并显示“原因”:“值不能为空。(参数'key')”
<PackageReference Include="FastEndpoints" Version="5.28.0" />
<PackageReference Include="FastEndpoints.Swagger" Version="5.28.0" />
这是我的 Program.cs
using FastEndpoints;
using FastEndpoints.Swagger;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddFastEndpoints();
builder.Services.AddControllers();
//builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
builder.Services.SwaggerDocument();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
//app.UseSwagger();
//app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseFastEndpoints();
app.UseSwaggerGen();
app.Run();
这是我的测试样本:
using FastEndpoints;
namespace WebApi.Features.SignUp
{
public class Endpoint : Endpoint<Request, Response>
{
public override void Configure()
{
Post("/signup");
AllowAnonymous();
}
public override async Task HandleAsync(Request r, CancellationToken c)
{
await SendAsync(new Response()
{
Message = $"hello {r.FirstName} {r.LastName}! your request has been received!"
});
}
}
public class GetArticle : EndpointWithoutRequest<Response>
{
public override void Configure() {
Get("/article/{ArticleID}");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
//http://localhost:5000/article/123
int articleID = Route<int>("ArticleID");
await SendAsync(new Response()
{
Message = $"hello {articleID}"
});
}
}
public class GetArticle2 : Endpoint<Article, Response>
{
public override void Configure()
{
Get("/article/{ArticleId}/{ArticleName}");
AllowAnonymous();
}
public override async Task HandleAsync(Article r, CancellationToken c)
{
await SendAsync(new Response()
{
Message = $"hello {r.ArticleId} {r.ArticleName}! "
});
}
}
public class GetArticle3 : EndpointWithoutRequest<Response>
{
public override void Configure()
{
Get("/article");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
//http://localhost:5000/article?id=123&name=artileA
string articleID = Route<int>("id", isRequired: false) == 0? "": Route<int>("id", isRequired: false).ToString();
string articleName = Route<string>("name", isRequired: false);
await SendAsync(new Response()
{
Message = $"hello {articleID} {articleName}"
});
}
}
public class Article {
public int ArticleId { get; set; }
public string ArticleName { get; set; }
}
public class Request
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
public class Response
{
public string Message { get; set; }
}
}
除了 public class GetArticle : EndpointWithoutRequest<Response>
中定义的 api 之外,以上所有内容都运行良好。当我在 swagger 中测试时,我会收到 400 错误,如下所示。看起来 Swagger 没有阅读我在 UI 中设置的文章 Id,这导致请求在端点内没有 id。但是由于是 get 请求,如果我点击
https://localhost:7043/article/123
,就可以很好地工作。因此,我建议为查询参数定义一个 DTO,就像我在
public class GetArticle2 : Endpoint<Article, Response>
中所做的那样。这个效果很好。