【问题标题】:ASP.NET CORE 2.2 Localization doesn't work on the serverASP.NET CORE 2.2 本地化在服务器上不起作用
【发布时间】:2020-03-16 15:34:22
【问题描述】:

我一直在尝试使用本地化在 .net core 2.2 上创建一个简单的博客网站。一切都在本地工作,但在服务器上,不起作用。

我在 AssemblyInfo.cs 文件中添加: [程序集:RootNamespace("e_cosmetics")]

这是 Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLocalization(options => options.ResourcesPath = "Resources");

            services.AddMvc()
                .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization();

            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            //services.AddDefaultIdentity<IdentityUser>()
            services.AddIdentity<User, IdentityRole>()
                .AddDefaultUI(UIFramework.Bootstrap4)
                .AddDefaultTokenProviders()
                .AddEntityFrameworkStores<ApplicationDbContext>();


            services.Configure<RequestLocalizationOptions>(
        opts =>
        {
            var supportedCultures = new List<CultureInfo>
            {
                new CultureInfo("en-US"),
                new CultureInfo("bg-BG")
            };

            opts.DefaultRequestCulture = new RequestCulture("bg-BG");
            // Formatting numbers, dates, etc.
            opts.SupportedCultures = supportedCultures;
            // UI strings that we have localized.
            opts.SupportedUICultures = supportedCultures;
        });

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

HomeController.cs

public class HomeController : Controller
{
    [HttpPost]
    public IActionResult SetLanguage(string culture, string returnUrl)
    {
        Response.Cookies.Append(
            CookieRequestCultureProvider.DefaultCookieName,
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
            new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
        );

        return LocalRedirect(returnUrl);
    }

_SelectLanguagePartial.cshtml

@inject IViewLocalizer Localizer
@inject IOptions<RequestLocalizationOptions> LocOptions

@{
    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var cultureItems = LocOptions.Value.SupportedUICultures
        .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
        .ToList();
}

<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
    <form id="selectLanguage" asp-controller="Home"
          asp-action="SetLanguage" asp-route-returnUrl="@Context.Request.Path"
          method="post" class="form-horizontal" role="form">
        @Localizer["Language:"] <select name="culture"
                                        asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"></select>
        <button type="submit" class="btn btn-default btn-xs">Save</button>

    </form>
</div>

关于.cshtml

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

<section class="about-wrapper">
    <div class="main-nav">
        <h1>@Localizer["За Нас"]</h1>

        <ul>
            <li><a asp-controller="Home" asp-action="index">@Localizer["Начало"]</a></li>
            <li><p>@Localizer["За Нас"]</p></li>
        </ul>
    </div>

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <globalization culture="bg-BG" uiCulture="bg-BG" />
  </system.web>
</configuration>

资源

Views.Home.about.en-US.resx

如果有人给我建议如何解决这个问题,我将非常感激。

【问题讨论】:

  • 服务器上string culture, string returnUrl的值是多少?你没有日志记录吗?
  • Train 我无权访问服务器日志。
  • 为什么不将错误记录到数据库中?还是文件?试着看看 Elmah。
  • @Train 我不知道这个应用程序。我安装并测试了它。我将新代码发布到服务器,但它没有显示任何问题。
  • 如果它在您的本地工作但在服务器上不起作用,显然服务器端发生了一些事情。如果没有关于服务器上发生的事情的更多信息,我很难提供帮助。您将不得不想办法从服务器记录更多信息。

标签: c# asp.net asp.net-mvc server localization


【解决方案1】:

我找到了解决问题的方法。

当项目名称不是有效的 .NET 标识符时,可能会发生这种情况。例如 my-project-name.csproj 将使用根命名空间 my_project_name 和程序集名称 my-project-name 导致此错误。 块引用

我更改了项目和文件夹的名称,现在可以使用了。

【讨论】:

    猜你喜欢
    • 2021-01-20
    • 2019-09-17
    • 1970-01-01
    • 2020-07-08
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多