In Program.cs I'm calling to AddRazorPages to setup Fluent Validation and some localization stuff. Unfortunately this has a lot of repetive code and I'm wondering if it is perfectly legal to reduce it by calling AddRazorPages twice?
I have tried to run the code and it seems to work, but can any one confirm the new version is effectively the same as the old version?
Current Program.cs
if (isDevelopment)
// Run-time compilation added during development
builder.Services.AddRazorPages(options => options.Conventions.Add(new CultureTemplatePageRouteModelConvention()))
.AddFluentValidation(fv =>
{
fv.RegisterValidatorsFromAssemblyContaining<PersonValidator>();
fv.LocalizationEnabled = true;
fv.DisableDataAnnotationsValidation = false;
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization()
.AddRazorRuntimeCompilation();
else
// No Run-time compilation needed when deployed
builder.Services.AddRazorPages(options => options.Conventions.Add(new CultureTemplatePageRouteModelConvention()))
.AddFluentValidation(fv =>
{
fv.RegisterValidatorsFromAssemblyContaining<PersonValidator>();
fv.LocalizationEnabled = true;
fv.DisableDataAnnotationsValidation = false;
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
New Program.cs
builder.Services.AddRazorPages(options => options.Conventions.Add(new CultureTemplatePageRouteModelConvention()))
.AddFluentValidation(fv =>
{
fv.RegisterValidatorsFromAssemblyContaining<PersonValidator>();
fv.LocalizationEnabled = true;
fv.DisableDataAnnotationsValidation = false;
})
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
// Run-time compilation added during development
if (isDevelopment)
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
CodePudding user response:
You can call AddRazorPages
as many times as you like. Internally, it uses a series of TryAdd*
calls to add the various services that the method is responsible for registering. If the service is already registered with the service container, nothing happens.