c# - Access x-requestid in HttpResponseMessage
问题描述
Please see the code below:
[HttpPut]
public async Task<IActionResult> CreatePerson([FromBody]CreatePersonCommand command, [FromHeader(Name = "x-requestid")] string requestId)
{
..
}
This is a web api method, which is accessed by another project.
The client code looks like this:
var response = await _httpClient.PutAsync(uri, personContent);
Is it possible to access the x-requestid at the client in the response that is sent from the web api project?
解决方案
If your HttpClient
comes from System.Net.Http
.
In case you need to get value:
var response = await _httpClient.PutAsync(uri, personContent);
response.Headers.TryGetValues("x-requestid", out var headerValues);
If you need to add headers to the response in your Web API, you have access to Response
entity in your controller which inherits from ASP ControllerBase
class:
Response.Headers.Add("x-requestid", "value");
推荐阅读
- qt - QGraphicsView additem() 越来越大
- azure - Azure 事件网格 Webhook 验证握手
- javascript - 根据另一个数组过滤一个数组。(使用反应)
- r - 根据不同日期变量的观测值的存在或缺失生成新的因子变量
- python - 是否有可以读取多个 excel 表但只有 sheet1 具有标题的 pandas 函数
- websphere-liberty - Websphere/Open-Liberty:访问 Weld-Internal 类
- vhdl - VHDL不受约束的std_logic_vector - 增加/减少索引的检测
- python-3.x - 在 Python 中将包含多个字符串的变量写入文本文件
- sql - 系统 i:系统名称 = 未知?
- switch-statement - Verilog switch 案例中的“非法引用内存”