preface

For the last week or two, I worked overtime, went back to work on my blog, switched my computer from Windows 10 to Ubuntu 18.10 and back to Windows 10, and didn’t make any progress in learning about the life cycle in Vue. We are going to update our.net Core 2.0 program to.NET Core 2.1. In my last blog post (ASP.NET Core: NET Core 2.0 project, which I wrote before, was deployed to a Linux server using Microsoft’s official recommended Nginx + Supervisor. The friends in the comment section of the blog park put forward that the way of using Docker can be more convenient to achieve, and it will be easier for novices to get started. Well, the beginner’s approach to deploying ASP.NET Core projects using Docker will also be covered in future articles. Welcome to pay attention.

The cause of

.net Core 2.1 was released on May 30th of this year. The release number is a minor update to the.net Core 2.0 version. It’s more about performance optimizations, changes to.NET Core Runtime,.NET Core Tools, adding apis or adding more system support.

In the last article, we built our.NET Core environment on a Linux server with the latest version of THE.NET Core Runtime installed and the.net Core version 2.0 deployed. Github.com/Lanesra712/…). Because Docker deployment is not adopted, the version difference between the program and the running environment may lead to some problems that we cannot reproduce in our program. Therefore, it is necessary to upgrade our program.

PS: If you are upgrading your.NET Core version for an actual production project, be careful, be careful, be careful!!

Step by Step

First, revise our project objective framework

In the process of updating VS 2017, our.NET Core version will also be updated. Of course, if you do not have the.NET Core 2.1 SDK installed on your computer, you will need to download the latest version of the SDK from the official website to install it.

Once we have installed our.NET Core 2.1 SDK, we can change the target framework for our original application to.NET Core 2.1.

By right-clicking our project, we can directly edit the CSPROj file or visually modify it by opening the properties option. So what we’re doing here is we’re actually editing our csPROj file through the properties page.

Replace Nuget package references

In version 2.1 of.NET Core, Microsoft replaced microsoft.aspnetcore. All, the basic DLL for.NET Core, with Microsoft.aspnetcore. App. After updating the application’s target framework we also need to remove the reference to microsoft.aspnetcore. All and add the reference to microsoft.aspnetcore.app.

The following Nuget packages are not included in Microsoft.aspnetcore. App. You can reference these packages in your project if you need them.

Microsoft.AspNetCore.ApplicationInsights.HostingStartup Microsoft.AspNetCore.AzureAppServices.HostingStartup Microsoft.AspNetCore.AzureAppServicesIntegration Microsoft.AspNetCore.DataProtection.AzureKeyVault Microsoft.AspNetCore.DataProtection.AzureStorage Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv Microsoft.AspNetCore.SignalR.Redis Microsoft.Data.Sqlite Microsoft.Data.Sqlite.Core Microsoft.EntityFrameworkCore.Sqlite  Microsoft.EntityFrameworkCore.Sqlite.Core Microsoft.Extensions.Caching.Redis Microsoft.Extensions.Configuration.AzureKeyVault Microsoft.Extensions.Logging.AzureAppServices Microsoft.VisualStudio.Web.BrowserLinkCopy the code

When referring to Microsoft.aspnetcore. App, you may be prompted that some dependencies are missing or that the version of the Nuget package you referenced is not sufficient for microsoft.aspnetcore. App. We just need to add the missing dependencies or upgrade the versions that don’t meet the requirements according to the error message.

For example, when I was upgrading the psu. EFCore class library, I found that the referenced assembly version did not meet our requirements. We used the 2.1.6 version of Microsoft.aspnetcore. App, and we only needed to upgrade the referenced DLL. Then install our latest version of Microsoft.aspnetcore.app.

Microsoft.DotNet.Watcher.Tools (dotnet watch)
Microsoft.EntityFrameworkCore.Tools.DotNet (dotnet ef)
Microsoft.Extensions.Caching.SqlConfig.Tools (dotnet sql-cache)
Microsoft.Extensions.SecretManager.Tools (dotnet user-secrets)
Copy the code

You can also remove this reference from the dotnet-aspnet-CodeGenerator Nuget package under the DotNetCliToolReference node. Use the global installation tool instead.

dotnet tool install -g dotnet-aspnet-codegenerator
Copy the code

Changes to code conventions based on ASP.NET Core 2.1

Since.net Core was upgraded to version 2.1, ASP.NET Core has been updated accordingly, and some of the basic code in the templates we created has been changed. For example, in the following example, the program.cs code structure in the MVC project we created using.NET Core 2.0 is somewhat different from the template code generated using.NET Core 2.1.

ASP.NET Core 2.0 namespace WebApp1 {public class Program {public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); }}Copy the code
ASP.NET Core 2.1 namespace WebApp1 {public class Program {public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); }}Copy the code

Here we modify our program.cs code structure based on the latest version of the template code.

ASP.NET
Method allows applications to opt in or out of ASP.NET

Other changes

In the update to the ASP.NET Core MVC framework, we have also updated some of the referenced JS libraries. I will not update them here, but will add a reminder of the GDPR policy for our application. And require our program to be accessed in HTTPS form.

First we create a distribution view _CookieConsentPartial to indicate that we need to collect user information, add an Action to the SecretController to display our privacy policy, and reference the created distribution view in our template page. I’m not going to make any changes to the style here, just to give you an example.

@using Microsoft.AspNetCore.Http.Features @{ var consentFeature = Context.Features.Get<ITrackingConsentFeature>(); var showBanner = ! consentFeature? .CanTrack ?? false; var cookieString = consentFeature? .CreateConsentCookie(); } @if (showBanner) { <nav id="cookieConsent" class="navbar navbar-default navbar-fixed-top" role="alert"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#cookieConsent .navbar-collapse"> <span class="sr-only">Toggle cookie consent banner</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <span class="navbar-brand"><span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span></span> </div> <div class="collapse navbar-collapse"> <p class="navbar-text"> Use this space to summarize your privacy and cookie use policy. </p> <div class="navbar-right"> <a asp-controller="Secret" asp-action="Privacy" class="btn btn-info navbar-btn">Learn More</a> <button type="button" class="btn btn-default navbar-btn" data-cookie-string="@cookieString">Accept</button> </div> </div> </div> </nav> <script> (function () { document.querySelector("#cookieConsent button[data-cookie-string]").addEventListener("click", function (el) { document.cookie = el.target.dataset.cookieString; document.querySelector("#cookieConsent").classList.add("hidden"); }, false); }) (); </script> }Copy the code
<partial name="_CookieConsentPartial" />Copy the code
/// </summary> /// </summary> /// </returns> [AllowAnonymous] public IActionResult Privacy() {return View(); }Copy the code

conclusion

From.net Core 2.0 to.net Core 2.1, the overall project has not changed much. When we release the project, we can find that the size of the project has been greatly reduced and the support for standalone deployment has been provided. We can get some great new features without too much work. Overall, it’s worth upgrading.

2. Independent Deployment (SCD) : The released project should contain all the components (.NET Core environment, third-party dependencies, program code) required by the released program, independent of the.NET Core environment on the target server system.

Of the pit

Personal Profile: Born in 1996, born in a fourth-tier city in Anhui province, graduated from Top 10 million universities. .NET programmer, gunslinger, cat. It will begin in December 2016. NET programmer career, Microsoft. NET technology stalwart, aspired to be the cloud cat kid programming for Google the best. NET programmer. Personal blog: yuiter.com blog garden blog: www.cnblogs.com/danvic712