0

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();
Lindstrøm
  • 396
  • 2
  • 16
  • 1
    have you tried the code yet? though, i doubt it works as you hoped for. the thing is, `AddRazorPages()` and `AddRazorPages(options => options.Conventions...)` may create different stuff altogether. you should assign the first `AddRazorPages` (the one with long parameters) into a `var` then within the `if`, and call `AddRazorRuntimeCompilation();` on that variable. – Bagus Tesa Apr 26 '22 at 08:27
  • Reading the answer of Mike Brind made me investigate this further. Looking at the source code I see no reason for the order to have any effect as ``AddRazorPages()`` does not reference ``builder.services.Configure`` whereas ``AddRazorPages(options)`` does. but thx for the comment, it was great to be sure :-) – Lindstrøm Apr 26 '22 at 09:33

1 Answers1

1

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.

https://github.com/dotnet/aspnetcore/blob/207f2c39ee1eccbc339450570fe6297026ea549c/src/Mvc/Mvc.RazorPages/src/DependencyInjection/MvcRazorPagesMvcCoreBuilderExtensions.cs#L95

Mike Brind
  • 28,238
  • 6
  • 56
  • 88