简体   繁体   English

ASP.NET linux 上的核心 2.2 项目未找到卫星资源

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

We've created an ASP.NET Core MVC project.我们创建了一个 ASP.NET Core MVC 项目。 Instead of using the new Microsoft.AspNetCore.Localization structure, we used the familiar old school embedded resources.我们没有使用新的Microsoft.AspNetCore.Localization结构,而是使用了熟悉的老式嵌入式资源。 We created a default .resx (with a designer class behind it), plus localized .resx files.我们创建了一个默认的.resx (后面有一个设计器 class),以及本地化的.resx文件。 So we have Resource.resx + Resource.Designer.cs , Resource.NL.resx , Resource.DE.resx , Resource.FR.resx , etc etc所以我们有Resource.resx + Resource.Designer.csResource.NL.resxResource.DE.resxResource.FR.resx

Now, during development on Windows, and when hosting in IIS, all is fine.现在,在 Windows 上的开发过程中,以及在 IIS 中托管时,一切都很好。 But when we are trying to host on linux (proxying in apache), the resources are not found.但是当我们尝试在 linux 上托管(在 apache 中代理)时,找不到资源。 Only the default/invariant resource is returned.仅返回默认/不变资源。

Unfortunately, Google didn't give me any solutions:不幸的是,谷歌没有给我任何解决方案:

Just to give some code... our ConfigureServices in startup.cs:只是给出一些代码......我们在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();
    }

The Configure:配置:

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

And in case anyone is interested, here is the ViewLocalizationMiddleware:如果有人感兴趣,这里是 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);
        }
    }
}

So we copy to the server using ftp over ssh.所以我们使用 ftp 而不是 ssh 复制到服务器。 We've done a chmod -R ugo+rwx on the folder with the files, just to make sure anyone could access everything, but still no luck.我们已经对包含文件的文件夹执行了chmod -R ugo+rwx ,只是为了确保任何人都可以访问所有内容,但仍然没有运气。

Apache config for the application应用程序的 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>

Kestrel configuration:红隼配置:

[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

More info, here's the resource folder + attributes:更多信息,这里是资源文件夹 + 属性:

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

And the contents:以及内容:

-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

Does anyone have any suggestions to why on Linux the localized resources are not found?有没有人对为什么在 Linux 上找不到本地化资源有任何建议?

Remco雷姆科

Have a look at the file names of your resource files.查看资源文件的文件名。

Linux is case sensitive and therefore: Resource.NL.resx, Resource.DE.resx, Resource.FR.resx are not the same as Resource.nl.resx, Resource.de.resx, Resource.fr.resx Linux 区分大小写,因此:Resource.NL.resx、Resource.DE.resx、Resource.FR.resx 与 Resource.nl.resx、Resource.de.resx、Resource.fr.resx 不同

This could be the problem so please take a look at the files.这可能是问题所在,因此请查看文件。 Go to your folder where you have your resource files and check the exact names. Go 到您拥有资源文件的文件夹并检查确切名称。 If that is the case you can change this part of the code:如果是这种情况,您可以更改这部分代码:

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

So the issue is that the culture name is nl, so .net looks for a folder called nl.所以问题是文化名称是 nl,所以 .net 会查找一个名为 nl 的文件夹。

But the resources were called resource.NL.resx... the compiler doesn't interpret this, but literally takes this part of the resource name and creates the resource folder for it, with the same casing.但是资源被称为resource.NL.resx ...编译器不会解释这一点,而是从字面上获取资源名称的这一部分并为其创建资源文件夹,并使用相同的大小写。

So the solution was indeed renaming the files to resource.nl.resx, which results in nl (lowercase) as a folder for the localized resources.所以解决方案确实是将文件重命名为resource.nl.resx,这导致nl(小写)作为本地化资源的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM