Mastodon

Migrating from .Net Core 2 to 3

May 04, 2020 by Kolappan N

I recently migrated my .Net Core Boilerplate template from .Net Core 2.2 to 3.1. It is a relatively easy process. If you are planning to upgrade the .Net Core version in your project then I recommend checking this awesome guide from Microsoft.

Some of the major things to look out for in this migration are,

Routing

Configuration of Routing in Startup.cs has changed considerably. If your are running an API server with Controllers then change the following in code

public void ConfigureServices(IServiceCollection services)
{
    // services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    // replace the above with
    services.AddControllers();
    .....
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // app.UseMvc();
    // replace this with
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

JWT token authentication

If you are using JWT authentication, then you might want to add the package Microsoft.AspNetCore.Authentication.JwtBearer to your API project. Also, you will need to change the following in the Startup.cs,

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // make sure this is located in between, UseRouting & UseEndpoints
    app.UseAuthentication();
    app.UseAuthorization();
}

Environment

Replace IHostingEnvironment with IWebHostEnvironment in the Startup.cs.

Json Result

Microsoft moved the JsonResult into the Microsoft.AspNetCore.Mvc.Core assembly. This caused a unexpected problem for me where passing a model object into JsonResult resulted in empty object passed to the frontend. Replacing known Model object with a anonymous object solved this issue.