Whats New in Asp .Net core 2.1


.net core On May 30, 2018 Microsoft announced in a blog post ASP.NET Core 2.1.0 now available the release of ASP.net Core 2.1.

Whats new

When ever Microsoft releases a new version after installing it I like to create a new project with the new version. When you create a new solution in Visual studio and create a new project Microsoft has this sample dummy project that is often created for you. I like to use this as a best practice guideline to see what changes they have made in the default project creation.

startup.cs

ConfigureServices

The first thing i noticed was a new section in the startup.cs in the ConfigureServices method.
[wp_ad_camp_3]
Asp.net core 2.0

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

Asp.net core 2.1

public void ConfigureServices(IServiceCollection services)
        {
            services.Configure(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

I wonder if this change is as a result of GDPR?

Configure

Asp.net core 2.0

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

Asp.net core 2.1

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

So we have two new things here app.UseHttpsRedirection(); and app.UseCookiePolicy();

The Cookie Policy Middleware allow you to control global characteristics of cookie processing and hook into cookie processing handlers when cookies are appended or deleted. So due to adding CookiePolicyOptions we now need to include app.UseCookiePolicy();

So what is UseHttpsRedirection? If you browse to the app via http://localhost:5000 you get redirected to the HTTPS endpoint. Which again makes scene to have since we really want people using HTTPs.
[wp_ad_camp_5]

Program.cs

Asp.net core 2.0

public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup()
                .Build();
    }

Asp.net core 2.1

public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup();
    }

I have a feeling digging into the reason behind this change will take a bit of time i will get back to you

Conclusion

This is just a few of the changes that I have found in the release of ASP .net Core 2.1. I will edit this when find more.


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.