[英]Intercept caller language in ASP.NET Core Web.API
Is there a way to intercept the caller's language in the ASP.NET Core Web API in C#? 有没有办法在C#中的ASP.NET Core Web API中拦截调用者的语言?
I'm referring to the natural language of the caller. 我指的是呼叫者的自然语言。
I solved the issue with the following code : 我用以下代码解决了这个问题:
private string GetCulture()
{
var headers = this.Request.Headers;
if (headers.ContainsKey("Accept-Language"))
{
Microsoft.Extensions.Primitives.StringValues token;
headers.TryGetValue("Accept-Language",out token);
return token[0].ToString().Split(';')[0].Split(',')[0];
}
return ("en-GB");
}
Thanks to all. 谢谢大家。
By language if u mean natural languages (like English) the client can specify this in an http header content-language:en-us
如果您是指自然语言(例如英语),则按语言,客户端可以在http标头
content-language:en-us
指定此content-language:en-us
If you're referring to the programming language, there's no standard header defined in the http spec to do this. 如果您指的是编程语言,则http规范中没有定义标准的标头来执行此操作。
Some clients include a user-agent
header to tell you what kind of an app made the request. 一些客户端包含一个
user-agent
标头,以告诉您发出请求的是哪种应用程序。 But not all clients do this. 但并非所有客户都这样做。
If you have control over your callers, u can set your own http header when the callers make the request to denote the language. 如果您可以控制呼叫者,则在呼叫者发出表示语言的请求时,您可以设置自己的http标头。
For eg X-Caller-Language:cs
对于例如
X-Caller-Language:cs
And interpret this header on your web Api as “the client app is c#” 并在您的Web Api上将此标头解释为“客户端应用程序为c#”
And if you really want to enforce your callers to specify this header, you should check this header in your code and if it doesn't exist, you should return an error code 400 with instructions on what you expect. 并且,如果您确实要强制调用者指定此标头,则应在代码中检查此标头,如果不存在,则应返回错误代码400,其中包含有关您期望的说明。
In any case this shouldn't be of concern to the server. 在任何情况下,服务器都不必担心。 Your apis should be designed to be platform independent.
您的api应该设计为独立于平台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.