首页 > 解决方案 > 在每个请求中处理来自洋葱架构中标头的数据

问题描述

我在标题中有一些需要过滤数据的自定义键( UserId、language_Id )。我想要服务项目中的这些密钥。但这些密钥仅存在于请求中。

使服务项目中(标题)中的这些键可用的最佳方法是什么?

我的解决方案包含来自项目:

控制器

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 !!! 
    }}

我认为通用服务处理每个请求中的标头,并且所有服务都继承自该通用服务。那个怎么样?或者任何通用的想法不要重复我。

标签: c#asp.net-corewebapi

解决方案


推荐阅读