c# - 在每个请求中处理来自洋葱架构中标头的数据
问题描述
我在标题中有一些需要过滤数据的自定义键( UserId、language_Id )。我想要服务项目中的这些密钥。但这些密钥仅存在于请求中。
使服务项目中(标题)中的这些键可用的最佳方法是什么?
我的解决方案包含来自项目:
- Web API -> 请求中的自定义键标头。
- 实体
- 存储库
- 服务 -> 需要这些密钥在此处可用。
控制器
public class ProductController : BaseController
{
private readonly IProductServices _ProductServices;
public ProductController(IProductServices ProductServices)
{
_ProductServices = ProductServices;
}
[HttpPost("GetList")]
public async Task<ActionResult<BaseResponseDto<IQueryable<ProductDTO>>>> GetList(ProductsPagingReqestDTO requestDto)
{
Microsoft.Extensions.Primitives.StringValues value = new Microsoft.Extensions.Primitives.StringValues();
HttpContext.Request.Headers.TryGetValue("languageid", out value);
int userId = GetCurrentUserId();
LanguageId = int.Parse(value);
_ProductServices.LanguageId = LanguageId;
return await _ProductServices.GetList(requestDto);
}
服务:
public class ProductServices: IProductServices
{
private readonly IRepository<Product> _productRepository;
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
public ProductServices(IRepository<Product> productRepository, IUnitOfWork unitOfWork, IMapper mapper)
{
_productRepository = productRepository;
_unitOfWork = unitOfWork;
_mapper = mapper;
}
public async Task<BaseResponseDto<IQueryable<ProductDTO>>> GetList(ProductsPagingReqestDTO requestDto)
{
// Must get keys from headers to using it here !!!
}}
我认为通用服务处理每个请求中的标头,并且所有服务都继承自该通用服务。那个怎么样?或者任何通用的想法不要重复我。
解决方案
推荐阅读
- r - 如何在r中显示没有滚动条的完整图例?
- apache-tomee - TomEE 8 JDK 级别
- python - 有没有办法从请求中抓取图像链接?
- node.js - 如何制作像 hidester.com/proxy 这样的“网站内代理”?GitHub上已经有这样的东西了吗?
- pandas - 有没有办法可以在 pandas/python 的 csv 文件中使用双引号来处理 poeple?
- c# - 使用 Crystalreport 13.0.18 版本的 Dotnet 程序死机
- javascript - 窗口对象属性行为
- swift - 有没有办法观察超层?
- javascript - React useContext 返回默认值
- python - 在 Pandas 中执行日期比较时排除 NaT/Null 值