我有使用 ASP.NET Core API 开发的 Web API。每个传入请求都插入了一个自定义 header 值。例如x-correlationid
。 Controller 使用该值来记录和跟踪请求。
目前我正在读取每个 Controller 中的值,如下所示
[Route("api/[controller]")]
public class DocumentController : Controller
{
private ILogger<TransformController> _logger;
private string _correlationid = null;
public DocumentController(ILogger<DocumentController > logger)
{
_logger = logger;
_correlationid = HttpContext.Request.Headers["x-correlationid"];
}
[HttpPost]
public async Task<intTransform([FromBody]RequestWrapper request)
{
_logger.LogInformation("Start task. CorrelationId:{0}", _correlationid);
// do something here
_logger.LogInformation("End task. CorrelationId:{0}", _correlationid);
return result;
}
}
我认为这违反了 DI 规则。
我不想读取 Controller 构造函数中的值,而是想将值注入(inject) Controller 构造函数中。
或者
中间件能否读取 x-correlationid
并*以某种方式*
使其可供所有 Controller 使用,这样我们就不必将其注入(inject)到任何 Controller 中?
这里什么是更好的选择?
最佳答案
Instead of reading the value inside the controller's constructor, I want to inject the value in the controller's constructor.
您无法将值本身注入(inject) api Controller 的构造函数中,因为在构造时,HttpContext
将为 null
。
一个“注入(inject)式”选项是在您的操作中使用 FromHeaderAttribute
:
[HttpPost]
public async Task<int> Transform(
[FromBody]RequestWrapper request,
[FromHeader(Name="x-correlationid")] string correlationId)
{
return result;
}
Can middleware read the x-correlationid and somehow make it available to all the controllers so we don't have to inject it in any controller?
我认为中间件解决方案可能无法满足您的需求。相反,您可以创建一个派生自 Controller
的自定义基类,并让所有 Api Controller 都派生自该基类。
public class MyControllerBase : Controller
{
protected string CorrelationId =>
HttpContext?.Request.Headers["x-correlationid"] ?? string.Empty;
}
[Route("api/[controller]")]
public class DocumentController : MyControllerBase
{
private ILogger<TransformController> _logger;
public DocumentController(ILogger<DocumentController> logger)
{
_logger = logger;
}
[HttpPost]
public async Task<intTransform([FromBody]RequestWrapper request)
{
_logger.LogInformation($"Start task. CorrelationId:{CorrelationId}");
// do something here
_logger.LogInformation($"End task. CorrelationId:{CorrelationId}");
return result;
}
}
关于asp.net-core - 如何在 Controller 中注入(inject)HttpHeader值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39459705/