Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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!

like image 580
deathcat05 Avatar asked Feb 04 '26 08:02

deathcat05


1 Answers

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.

like image 134
jmoerdyk Avatar answered Feb 06 '26 00:02

jmoerdyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!