英文:
Use HttpContext in ASP.NET Core
问题
在.NET 4.6.2中,我们使用HttpContext如下:
HttpContext.Current.Request.Headers.Add("language", languageCode);
但在.NET 5和6中,相同的代码会失败。有没有替代方案?
英文:
in .net 4.6.2 we are using HttpContext like this
HttpContext.Current.Request.Headers.Add("language", languageCode);
But the same is faling in .net 5 and 6 What is the alternative for the above code.
答案1
得分: 1
I think the question has been answered here and here.
You can access the current request from the property HttpContext
from within a controller or inject IHttpContextAccessor
anywhere else in your code.
英文:
I think the question has been answered here and here.
You can access the current request from the property HttpContext
from within a controller or inject IHttpContextAccessor
anywhere else in your code.
答案2
得分: 0
Add from client side:
var httpClient = new HttpClient() { BaseAddress = uri };
httpClient.DefaultRequestHeaders.Add("language", "en");
英文:
Add from client side:
var httpClient = new HttpClient() { BaseAddress = uri };
httpClient.DefaultRequestHeaders.Add("language", "en");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论