Replacing UseMvc in .Net Core 3.0
Asked Answered
E

2

10

I'm trying to figure out how to properly replace app.UseMvc() code that use to be part .net core 2.2. The examples go so far as to tell me what are all the codes I can call but I'm not yet understanding which should I call. For example for my MVC Web Application I have the following:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseStatusCodePagesWithReExecute("/Error/Index");
    app.UseMiddleware<ExceptionHandler>();
    app.UseStaticFiles(new StaticFileOptions()
    {
        OnPrepareResponse = (context) =>
        {
            context.Context.Response.GetTypedHeaders()
                .CacheControl = new CacheControlHeaderValue
            {
                MaxAge = TimeSpan.FromDays(30)
            };
        }
    });

    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
        endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
    });
}

Before I would provide my routing inside the UseMvc() options. However now it seems I have to provide it inside MapControllerRoute But the examples always seem to also call MapRazorPages(). Do I need to call both or am I suppose to call just one? What is the actual difference between the two and how do I setup a default controller and a default action?

Esta answered 18/10, 2019 at 17:41 Comment(1)
One's for controllers (MVC) the other's for Razor Pages. They can each have their own route setup.Coupler
D
20

This is documented in the Migrate from ASP.NET Core 2.2 to 3.0 article. Assuming you want an MVC application.

The following example adds support for controllers, API-related features, and views, but not pages.

services
    // more specific than AddMvc()
    .AddControllersWithViews()
    .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)

And in Configure:

    public void Configure(IApplicationBuilder app)
    {
        app.UseStaticFiles();

        app.UseRouting();

        // The equivalent of 'app.UseMvcWithDefaultRoute()'
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapDefaultControllerRoute();
            // Which is the same as the template
            endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
        });
    }

For the order of use statemtents check the documentation.

Dvandva answered 19/10, 2019 at 20:24 Comment(0)
G
1

The easiest way to fix it... Build a new project targeting the .NET Core that you need and just copy the new Configure method and paste into your project that you are migrating to...

In this example... Here are the old code lines:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseMvc();
}

And here are the new code lines:

// This method gets called by the runtime. Use this method to configure 
the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}
Georgiana answered 29/12, 2021 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.