Bagzli
Bagzli

Reputation: 6579

Replacing UseMvc in .Net Core 3.0

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?

Upvotes: 11

Views: 19613

Answers (2)

Jackson Calixto
Jackson Calixto

Reputation: 11

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();
    });
}

Upvotes: 1

user4864425
user4864425

Reputation:

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.

Upvotes: 21

Related Questions