【问题标题】:Change culture for HttpResponse messages and exceptions in Asp.Net在 Asp.Net 中更改 HttpResponse 消息和异常的文化
【发布时间】:2021-08-23 13:50:44
【问题描述】:

我正在尝试在英文版的 ASP.Net 应用程序中显示所有错误消息和响应。 这是我尝试过的(在 Configure 方法的开头):

        CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
        CultureInfo.CurrentUICulture = CultureInfo.CurrentCulture;
        CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CurrentCulture;
        CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CurrentCulture;
        System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CurrentCulture;
        System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture;

        var supportedCultures = new[] { "en-US" };
        var localizationOptions = new RequestLocalizationOptions()
            .SetDefaultCulture(supportedCultures[0])
            .AddSupportedCultures(supportedCultures)
            .AddSupportedUICultures(supportedCultures);
        localizationOptions.DefaultRequestCulture = new RequestCulture(supportedCultures[0]);

        app.UseRequestLocalization(localizationOptions);

这是我返回的异常消息(我正在使用 Flulrl,但使用 HttpClient 会得到相同的结果):

resp = await _flurlClient
                .Request("Accounts", "Login")
                .PostJsonAsync(loginModel);

Call failed. Impossibile stabilire la connessione. Rifiuto persistente del computer di destinazione. (localhost:5001)...

我尝试在 Kestrel 和 IIS Express 上托管,但没有任何变化。 我错过了什么?

【问题讨论】:

  • 你忘了加services.AddLocalization();吗?
  • @GordonKhanhNg。我试过了,但没有帮助。我不想提供我自己的本地化。我只想得到默认错误,就好像我有一个英文本地化的操作系统一样。这可能吗?
  • 哦……我仔细看了看,发现了一些……但这与这里的评论完全不符……所以看看答案

标签: c# asp.net-core localization dotnet-httpclient cultureinfo


【解决方案1】:

您缺少 2 个部分:

  • 添加services.AddLocalization();,这将为您注册IStringLocalizerFactory 和通用IStringLocalizer
  • app.UseRequestLocalization 使用 IOptions<RequestLocalizationOptions>,您目前也缺少它。

所以代码应该是:

// Registering things
services.AddLocalization();

var supportedCultures = new[]
    {
        new CultureInfo("en-US")
    };

services.Configure<RequestLocalizationOptions>(opts =>
    {
        opts.DefaultRequestCulture = new RequestCulture("en-US");
        opts.SupportedCultures = supportedCultures;
        opts.SupportedUICultures = supportedCultures;

        opts.AddInitialRequestCultureProvider(new CustomRequestCultureProvider(context =>
        Task.FromResult(new ProviderCultureResult("en-US"))));
    });

// Using things
app.UseRequestLocalization();

注意:由于您只想返回英文本地化作为示例,我刚刚将配置设置为“en-US”将是您唯一的选择。如果您需要真正的本地化,请在var supportedCulturesopts.AddInitialRequestCultureProvider 使用您自己的逻辑

【讨论】:

  • SupportedCultures 想要 CultureInfos 而不是字符串,除此之外它应该可以工作。我明天再测试一下,让你知道。谢谢;D
  • 好点...我只是从您的代码中复制该部分(XD)...仅修复了该部分。有信心吗朋友
  • 我又试了一次,但还是不行。我收到此错误:System.Net.Http.HttpRequestException: Impossibile stabilire la connessione. Rifiuto persistente del computer di destinazione. (localhost:4545) ---&gt; System.Net.Sockets.SocketException (10061): Impossibile stabilire la connessione. Rifiuto persistente del computer di destinazione.
  • 这是我对 HttpClient 所做的:await _client.GetAsync("https://localhost:4545/test"); url 是假的。我不知道发生了什么,我以为它可以工作,但现在不行......我尝试了 IIS Express 和 Kestrel
  • Impossibile stabilire la connessione 这个异常指出httpClient 无法调用服务器(无法向服务器发出请求)。它与定位器无关。顺便说一句...这就是my project 的设置...就像一个魅力
猜你喜欢
  • 1970-01-01
  • 2014-01-22
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
相关资源
最近更新 更多