简体   繁体   English

ASP.NET Core Web.API中的拦截调用者语言

[英]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.

相关问题 ASP.NET Core 2.1 Web.API更改应用程序见解的日志记录检测键 - ASP.NET Core 2.1 Web.API change application insights instrumentation key for logging 带有WEB.API和Castle Windsor容器的ASP.NET MVC - ASP.NET MVC with WEB.API and Castle Windsor container 使用 asp.net web.api json 不起作用 - Consuming asp.net web.api json not working WEB API + ASP.NET尝试以json格式显示来自WEB.API的数据 - WEB API + ASP.NET trying to display data from WEB.API in json format 我可以在Asp.net Web.API中的AppStart一次自定义RequestTelemetry属性吗? - Can I customize RequestTelemetry properties once at AppStart in Asp.net Web.API ASP.NET web.api帮助页面不显示任何描述 - ASP.NET web.api Help Page does not show any description 如何在AuthorAttribute的ASP.NET Web.API MVC 5的IsAuthorized上获取Post参数 - How to get Post parameters on IsAuthorized of AuthorizeAttribute ASP.NET Web.API MVC 5 即使具有自定义验证属性,Asp.net Web.APi也会返回内置错误响应 - Asp.net Web.APi returns build in error response even with custom validation attribute 如何在ASP.NET Web.Api中运行后台任务? - How do I run Background Tasks in ASP.NET Web.Api? 使用Asp.net Webforms中的属性和web.api重定向到网页 - Redirect to a webpage using an attribute in Asp.net webforms with web.api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM