Reputation: 103
I've been having some trouble with decimal numbers in asp net core mvc. I got it to work in a regular asp net app by adding this to the web.config:
<system.web>
...
<globalization uiCulture="en" culture="en-US"/>
</system.web>
But, as there isn't a web.config present in the core app, I'm not quite sure what to do. What would the closest approximation of this in core look like?
Upvotes: 5
Views: 11884
Reputation: 10468
In .NET 7.0, I got the culture changed globally in my ASP.NET app by sticking 3 lines in my "Program.cs", see below:
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.HttpOverrides;
using System.Globalization;
var builder = WebApplication.CreateBuilder(args);
var cultureInfo = new CultureInfo("en-AU");
//cultureInfo.NumberFormat.CurrencySymbol = "€";
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
builder.Services.AddControllers();
...
Upvotes: 2
Reputation: 12685
In Asp.Net Core ,localization is configured in the Startup.ConfigureServices method and can be used throughout the application:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
The current culture on a request is set in the localization Middleware. The localization middleware is enabled in the Startup.Configure
method. The localization middleware must be configured before any middleware which might check the request culture (for example, app.UseMvcWithDefaultRoute()
).
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});
app.UseStaticFiles();
// To configure external authentication,
// see: http://go.microsoft.com/fwlink/?LinkID=532715
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
For more details , you could refer to the official documentation .
Upvotes: 7