簡體   English   中英

ASP.NET linux 上的核心 2.2 項目未找到衛星資源

[英]ASP.NET Core 2.2 project on linux not finding satelite resources

我們創建了一個 ASP.NET Core MVC 項目。 我們沒有使用新的Microsoft.AspNetCore.Localization結構,而是使用了熟悉的老式嵌入式資源。 我們創建了一個默認的.resx (后面有一個設計器 class),以及本地化的.resx文件。 所以我們有Resource.resx + Resource.Designer.csResource.NL.resxResource.DE.resxResource.FR.resx

現在,在 Windows 上的開發過程中,以及在 IIS 中托管時,一切都很好。 但是當我們嘗試在 linux 上托管(在 apache 中代理)時,找不到資源。 僅返回默認/不變資源。

不幸的是,谷歌沒有給我任何解決方案:

只是給出一些代碼......我們在startup.cs中的ConfigureServices

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<RequestLocalizationOptions>(options =>
        {
            options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("nl");
            options.SupportedCultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures);
            options.AddSupportedUICultures("nl", "de", "fr");
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        services.AddHttpContextAccessor();
    }

配置:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseForwardedHeaders();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRequestLocalization();
        app.UseMiddleware<ViewLocalizationMiddleware>();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Scan}/{action=Index}/{id?}");
        });            
    }

如果有人感興趣,這里是 ViewLocalizationMiddleware:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ConsumerWebsite.Business
{
    public class ViewLocalizationMiddleware
    {
        private readonly Microsoft.AspNetCore.Http.RequestDelegate _next;

        public ViewLocalizationMiddleware(Microsoft.AspNetCore.Http.RequestDelegate next)
        {
            _next = next;
        }

        public async System.Threading.Tasks.Task Invoke(Microsoft.AspNetCore.Http.HttpContext context)
        {
            var requestCulture = context.Features.Get<Microsoft.AspNetCore.Localization.IRequestCultureFeature>();

            if (requestCulture != null)
            {
                System.Globalization.CultureInfo.CurrentCulture = requestCulture.RequestCulture.Culture;
                System.Globalization.CultureInfo.CurrentUICulture = requestCulture.RequestCulture.UICulture;
            }

            await _next.Invoke(context);
        }
    }
}

所以我們使用 ftp 而不是 ssh 復制到服務器。 我們已經對包含文件的文件夾執行了chmod -R ugo+rwx ,只是為了確保任何人都可以訪問所有內容,但仍然沒有運氣。

應用程序的 Apache 配置

<VirtualHost *:80>
        ServerName some.domain.com
        Redirect permanent / https://some.domain.com/
        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING}
#        Header unset Server
#        Header add Server "TLS web server"
</VirtualHost>

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5012/
    ProxyPassReverse / http://127.0.0.1:5012/
    ServerName some.domain.com
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/some.domain.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/some.domain.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/some.domain.com/chain.pem
</VirtualHost>
</IfModule>

紅隼配置:

[Unit]
Description=Our ASP.Core 2.2 application

[Service]
WorkingDirectory=/home/websitehome/
ExecStart=/usr/bin/dotnet "/home/websitehome/website.dll"
Restart=always
# Restart service after 10 seconds if dotnet service crashes
RestartSec=10
SyslogIdentifier=messages
User=apache
Environment=ASPNETCORE_ENVIRONMENT=Acceptance
Environment=ConnectionStrings__Master=SERVER=127.0.0.1;DATABASE=database;UID=uid;PWD=password;SslMode=none;

[Install]
WantedBy=multi-user.target

更多信息,這里是資源文件夾 + 屬性:

drwxrwxr-x   2 remco    remco       4096 Oct  2 22:14 NL

以及內容:

-rw-rw-r-- 1 remco    remco    6144 Oct  2 22:14 Base.resources.dll
-rw-rw-r-- 1 remco    remco    5120 Oct  2 22:14 Website.resources.dll
-rw-rw-r-- 1 remco    remco    6144 Oct  2 22:14 APIAccess.resources.dll

有沒有人對為什么在 Linux 上找不到本地化資源有任何建議?

雷姆科

查看資源文件的文件名。

Linux 區分大小寫,因此:Resource.NL.resx、Resource.DE.resx、Resource.FR.resx 與 Resource.nl.resx、Resource.de.resx、Resource.fr.resx 不同

這可能是問題所在,因此請查看文件。 Go 到您擁有資源文件的文件夾並檢查確切名稱。 如果是這種情況,您可以更改這部分代碼:

options.AddSupportedUICultures("nl", "de", "fr");

所以問題是文化名稱是 nl,所以 .net 會查找一個名為 nl 的文件夾。

但是資源被稱為resource.NL.resx ...編譯器不會解釋這一點,而是從字面上獲取資源名稱的這一部分並為其創建資源文件夾,並使用相同的大小寫。

所以解決方案確實是將文件重命名為resource.nl.resx,這導致nl(小寫)作為本地化資源的文件夾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM