C# 对于asp.net core 2 graphql端点,如何提取请求头并将其传递给业务逻辑?
我使用asp.net web api 2和EntityFramework 6开发了以下代码段C# 对于asp.net core 2 graphql端点,如何提取请求头并将其传递给业务逻辑?,c#,graphql,asp.net-core-2.1,graphql-dotnet,C#,Graphql,Asp.net Core 2.1,Graphql Dotnet,我使用asp.net web api 2和EntityFramework 6开发了以下代码段 public class TestController : BaseApiController { private readonly ITestService _testService; private readonly ICommonService _commonService; private readonly IImageService _imageService;
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;
}
}
公共类TestController:BaseApiController
{
专用只读ITestService(测试服务);
私有只读ICommonService\u commonService;
专用只读IImageService _imageService;
公共测试控制器(ITestService testService、ICommonService commonService、IImageService imageService)
{
_testService=testService;
_commonService=commonService;
_imageService=imageService;
}
[路线(“测试”)]
public IHttpActionResult Get()
{
var resp=_testService.GetDetailsForLocation(区域设置);
返回Ok(resp);
}
}
公共类BaseApiController:ApicController
{
公共字符串区域设置
{
得到
{
if(Request.Headers.Contains(“接受语言”))
{
return Request.Headers.GetValues(“接受语言”).First();
}
其他的
{
返回字符串。空;
}
}
}
公共字符串GetCookieId()
{
字符串值=string.Empty;
IEnumerable cookies=this.Request.Headers.GetCookies(“mycokie”);
if(cookies.Any())
{
IEnumerable cookie=cookies.First().cookies;
if(cookie.Any())
{
var cookieValue=cookie.FirstOrDefault(x=>x.Name==“mycookie”);
if(cookieValue!=null)
value=cookieValue.value.ToLower();
}
}
返回值;
}
}
我正在使用asp.net core 2和graphql.net将现有restapi端点转换为graphql端点。在下面的方法中,目前我正在发送“en”作为值,但我希望传递区域设置值,就像在上述实现中asp.net web api 2的情况下一样
在这里,我想知道读取请求头并将值传递给业务loigc(即在本例中传递给方法:GetDetailsForLocation(“en”)的最佳方法是什么
公共类TestQuery:ObjectGraphType
{
公共测试查询(ITestService测试服务)
{
字段(“结果”,解析:context=>testService.GetDetailsForLocation(“en”),描述:“测试数据”);
}
}
有人能帮我提供解决问题的指导吗?最简单的方法是使用
IHttpContextAccessor
。注册IHttpContextAccessor
在StartUp.cs
中:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
public void配置服务(IServiceCollection服务)
{
services.AddSingleton();
}
GraphQL类:
public class TestQuery : ObjectGraphType<object>
{
public TestQuery(ITestService testService, IHttpContextAccessor accessor)
{
Field<TestResultType>(
"result",
description: "Test data",
resolve: context => testService.GetDetailsForLocation(accessor.HttpContext...)
);
}
}
公共类TestQuery:ObjectGraphType
{
公共TestQuery(ITestService testService、IHttpContextAccessor访问器)
{
场(
“结果”,
描述:“测试数据”,
解析:context=>testService.GetDetailsForLocation(accessor.HttpContext…)
);
}
}
感谢Joe的快速回复。看到您的回复,我非常激动和高兴:)
public class TestQuery : ObjectGraphType<object>
{
public TestQuery(ITestService testService, IHttpContextAccessor accessor)
{
Field<TestResultType>(
"result",
description: "Test data",
resolve: context => testService.GetDetailsForLocation(accessor.HttpContext...)
);
}
}