app.MapPost($"{root}/v1/AuthenticateUser"
    , async Task<Results<Ok<GetTokenForResponse>, BadRequest<ErrorModel>>> 
    (HttpContext httpContext, IMediator mediator, AuthenticateBody body) =>
{
    var response = await mediator.Send(new AuthenticateUserHandler.Request(httpContext, body.UserName, body.Password));
    if (response.IsSuccessful)
        return TypedResults.Ok(response.Result);
    return TypedResults.BadRequest(response.Error);
})
.WithTags(root);

I am currently running on .NET 7
I am not finding any clues as to why the BadRequest method will NOT return the body
Results.BadRequest Method on MS Docs

似乎这应该返回在GET GO中定义的任何对象.但出于某种原因,事实并非如此.100

public record BaseResponse<T>
{
    public HttpContext? HttpContext { get; set; }
    public bool IsSuccessful => Error is null && Result is not null;
    public ErrorModel? Error { get; set; }
    public T? Result { get; set; }
}

在本例中,T属于一个名为GetTokenFor_Response的类.Ok路径返回有效的正文.BadRequest并非如此.

NOTE: Adding screenshot of Response object coming back from Mediatr: enter image description here

注2: 问题在Github点开始.

NOTE 3: Changed the Error object like this and it now works. The error was not in the BadRequest method per se. It was a record that did not have public getters? I think? enter image description here

推荐答案

在我的代码中,记录定义似乎是足够的,但是当通过BadRequest方法发送时,一些东西在转换中丢失了.我猜测,强调猜测,没有显式定义的公共getter/setter是最终的罪魁祸首.

The following changes in the error object definition fixed this issue: enter image description here

请参阅@Guru-Stron的回复.帮我填了几个空白处.

.net相关问答推荐

升级到.NET8后,SignalR(在坞站容器上)网关损坏

通过交互服务器渲染模式和流渲染的组合防止双重渲染

CLR如何在后台优化布尔比较操作?

避免函数和其他对象之间的相互递归的模式?

有没有更好的方法使用 Pusher Channels 的批事件发布消息?

Visual Studio 2022 中的目标操作系统和目标运行时有什么区别?

向从 .NET 序列化的对象添加 Xml 属性

是否存在指定的(子)索引分隔符?

dotnet ef dbcontext scaffold command --data-annotations 或 -d 命令行参数似乎不起作用

将毫秒转换为人类可读的时间间隔

如何判断 IOException 是否为 Not-Enough-Disk-Space-Exception 类型?

在一个 LINQ 查询中获取两列的总和

.NET 应用程序的链接器状态(又名请先生,我可以有一个链接器2009 年版)

返回 IQueryable 或不返回 IQueryable

参数命名:文件名还是文件名?

如何在 C# 中将 null 值设置为 int?

用 double 替换 int 时的 Visual Studio 长编译

如何在 C# 中处理 XML

/langversion 的错误选项6无效;必须是 ISO-1、ISO-2、3、4、5 或默认值

如何将我的应用程序窗口置于最前面?