using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using System.Collections.Generic;
[ApiController]
[Route("[controller]")]
public class MyController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
// 获取单个请求头
StringValues headerValue = HttpContext.Request.Headers["HeaderName"]; // 替换"HeaderName"为你想获取的头名称
// 获取所有请求头
IHeaderDictionary headers = HttpContext.Request.Headers;
// 将请求头存入字典
Dictionary<string, string> headersDict = new Dictionary<string, string>();
foreach (var header in headers)
{
headersDict[header.Key] = header.Value;
}
// 根据需要处理headerValue, headers, 或 headersDict
// ...
return Ok(); // 返回你的响应
}
}