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?
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();
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();
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.