gpt4 book ai didi

asp.net-web-api - 调解器 Api 调用失败

转载 作者:行者123 更新时间:2023-12-04 17:42:13 24 4
gpt4 key购买 nike

我正在尝试使用调解器和 .net 核心发出一个简单的请求。我收到一个我不理解的错误。我想要做的只是一个简单的调用来取回 guid。

基础 Controller :

[Route("api/[controller]/[action]")]
[ApiController]
public class BaseController : Controller
{
private IMediator _mediator;
protected IMediator Mediator => _mediator ?? (_mediator = HttpContext.RequestServices.GetService<IMediator>());
}

Controller :

// GET: api/Customer/username/password
[HttpGet("{username}/{password}", Name = "Get")]
public async Task<ActionResult<CustomerViewModel>> Login(string username, string password)
{
return Ok(await Mediator.Send(new LoginCustomerQuery { Username = username,Password = password }));
}

查询:

public class LoginCustomerQuery : IRequest<CustomerViewModel>
{
public string Username { get; set; }
public string Password { get; set; }
}

查看模型:

 public class CustomerViewModel
{
public Guid ExternalId { get; set; }
}

处理程序:

public async Task<CustomerViewModel> Handle(LoginCustomerQuery request, CancellationToken cancellationToken)
{
var entity = await _context.Customers
.Where(e =>
e.Username == request.Username
&& e.Password == Encypt.EncryptString(request.Password))
.FirstOrDefaultAsync(cancellationToken);
if (entity.Equals(null))
{
throw new NotFoundException(nameof(entity), request.Username);
}

return new CustomerViewModel
{
ExternalId = entity.ExternalId
};
}

这是我遇到的异常: enter image description here

请告诉我您还需要什么来确定可能是什么问题。另外,请善待我,我已经离开 C# 一段时间了。

最佳答案

感谢您提供信息,这是缺少的 DI。我加了这个

// Add MediatR
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
services.AddMediatR(typeof(LoginCustomerQueryHandler).GetTypeInfo().Assembly);

我们准备好了。

关于asp.net-web-api - 调解器 Api 调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53950930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com