deathcat05
deathcat05

Reputation: 469

.AddMvc() in ASP.NET Core 3.0?

I am migrating an ASP.NET Core 2.2 web application over to 3.0, and have a clarification question on .AddMvc(). My Application uses Razor Pages and Views, if that's important.

So currently, I have the following in the 2.2 code:

services.AddMvc()
            .AddRazorPagesOptions(options =>
            {
                options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
            })
           .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

The Migration Docs say that "AddMvc continues to behave as it has in previous releases." But, then it goes on to say that the following is "the same thing as .AddMvc() in 2.2":

    services.AddControllers();
    services.AddRazorPages();

Therefore, my question is, which one should I use?

My approach is to do something like:

    services.AddControllersWithViews();
    services.AddRazorPages()
            .AddRazorPagesOptions(options =>
             {
                 options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
              })
              .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

Would this be the correct way?

Thanks!

Upvotes: 3

Views: 9598

Answers (1)

jmoerdyk
jmoerdyk

Reputation: 5518

No, I believe that you would set the Razor page options in the .AddRazorPages() call like so:

services.AddControllersWithViews();
services.AddRazorPages(options => 
{
    options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
});

I'm not certain the .SetCompatibilityVersion() is necessary, but according to the Intellisense that you can hang it off of either the .AddControllersWithViews() or .AddRazorPages() call.

Upvotes: 11

Related Questions