使用HttpContext在ASP.NET Core中

huangapple go评论51阅读模式
英文:

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");

huangapple
  • 本文由 发表于 2023年3月31日 19:51:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定