.net-coreiislocalizationglobalization

Globalization of ASP.NET Core 6 MVC application


I have developed an ASP.NET Core 6 MVC application with globalization, I've created the role folder structure for Views/Controller/ViewModels .resx for each language.

While debugging running on IIS Express from Visual Studio or accessing it via localhost in the IIS Server the localization is working fine.

But when I try to access it from outside by the hostname, it doesn't change the language. I've searched a lot, added parameters to the web.config, but it didn't work. Could someone give a hand?

This is the program.cs from the application

using FastReport.Data;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using MYAPP;
using MYAPP.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");

builder.Services.AddRazorPages().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix).AddDataAnnotationsLocalization();

builder.Configuration
       .SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
builder.Services.AddDbContext<SncMesSloRpcContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DatabaseContext")));
builder.Services.AddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));

var app = builder.Build();

var supportedCultures = new[] { "en", "pt", "es" };

var localizationOptions = new RequestLocalizationOptions()
    .AddSupportedCultures(supportedCultures)
    .AddSupportedUICultures(supportedCultures)
    .SetDefaultCulture("es");

localizationOptions.RequestCultureProviders = new List<IRequestCultureProvider>()
    {
        new QueryStringRequestCultureProvider(),
        new CookieRequestCultureProvider(),
        new AcceptLanguageHeaderRequestCultureProvider()
    };
app.UseRequestLocalization(localizationOptions);

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Login}/{action=Index}");
    //pattern: "{controller=Dashboards}/{action=Index}");

app.Run();

Solution

  • After many attempts at IIS settings and changes to the program.cs and startup.cs files, I discovered that the problem of the location not working in HTTP, but working in HTTPS, was due to the cookie setting being fixed in the page's login controller. I changed this setting and now it's working. Thanks to those who tried to help, instead of just editing the post to inflate their ego. Here's the code working:

     Response.Cookies.Append(
                        CookieRequestCultureProvider.DefaultCookieName,
                        CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
                        new CookieOptions { Expires = DateTimeOffset.UtcNow.AddMinutes(1), HttpOnly = false, Secure = false, Path = "/" });