代码之家  ›  专栏  ›  技术社区  ›  santosh kumar patro

对于asp.net core 2 graphql端点,如何提取请求头并将其传递到业务逻辑?

  •  0
  • santosh kumar patro  · 技术社区  · 7 年前

    我使用asp.net web api 2和EntityFramework 6开发了以下代码段。

    public class TestController : BaseApiController
    {
        private readonly ITestService _testService;
        private readonly ICommonService _commonService;
        private readonly IImageService _imageService;
        public TestController(ITestService testService, ICommonService commonService, IImageService imageService)
        {
            _testService = testService;
            _commonService = commonService;
            _imageService = imageService;
        }
    
        [Route("test")]
        public IHttpActionResult Get()
        {
            var resp = _testService.GetDetailsForLocation(locale);
            return Ok(resp);
        }
    }
    
    public class BaseApiController : ApiController
    {
        public string locale
        {
            get
            {
                if (Request.Headers.Contains("Accept-Language"))
                {
                    return Request.Headers.GetValues("Accept-Language").First();
                }
                else
                {
                    return string.Empty;
                }
            }
        }
    
        public string GetCookieId()
        {
            string value = string.Empty;
            IEnumerable<CookieHeaderValue> cookies = this.Request.Headers.GetCookies("mycookie");
            if (cookies.Any())
            {
                IEnumerable<CookieState> cookie = cookies.First().Cookies;
                if (cookie.Any())
                {
                    var cookieValue = cookie.FirstOrDefault(x => x.Name == "mycookie");
                    if (cookieValue != null)
                        value = cookieValue.Value.ToLower();
                }
            }
    
            return value;
        }
    }
    

    在这里,我想知道读取请求头并将值传递给businessloigc(即在本例中传递给方法:GetDetailsForLocation(“en”)的最佳方法是什么

    public class TestQuery : ObjectGraphType<object>
    {
        public TestQuery(ITestService testService)
        {
            Field<TestResultType>("result", resolve: context => testService.GetDetailsForLocation("en"), description: "Test data");
        }
    }
    

    有人能帮我解决这个问题吗?

    1 回复  |  直到 7 年前
        1
  •  6
  •   Joe McBride    7 年前

    最简单的方法是 IHttpContextAccessor IHTTP上下文访问器 作为独生子女。

    https://adamstorr.azurewebsites.net/blog/are-you-registering-ihttpcontextaccessor-correctly

    在 StartUp.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    }
    

    public class TestQuery : ObjectGraphType<object>
    {
        public TestQuery(ITestService testService, IHttpContextAccessor accessor)
        {
            Field<TestResultType>(
                "result",
                description: "Test data",
                resolve: context => testService.GetDetailsForLocation(accessor.HttpContext...)
            );
        }
    }