Introduction
With 20 percent faster performance than .NET Core 1.X, .NET Core 2.0 is in the limelight. Optimized to the core, .NET Core 2.0 is the next big thing for Microsoft developers. This is a modular framework uniting the MVC and Web API into a single programming model running on .NET Framework or .NET Core run-time but can run on any platform. It is highly flexible in deploying to the cloud and since cross platform, and its the demand among developers increases.
Asp.Net Vs Asp.Net Core
Asp.Net core application is also a .Net Framework application because the .Net core libraries are used for both the core and .Net framework applications. Here the thing to notice is Asp.Net core is built on top of both .Net framework & .Net Core whereas the Asp.Net is now being termed as .Net Framework. Check the image below (from Blogs )
Asp.net Core libraries/dependencies are self-contained, most of the packages are downloaded from Nuget but can run on windows, Linux & Mac whereas in Asp.Net the packages are self contained but can only run on windows based on the .Net Core CLI (Command Line Interface) on which the IDE (Integrated Development Environment) relies on. It is the cross platform tool chain for developing .Net applications irrespective of the platform. Visual Studio as IDE has support for running on Windows and MacOS. Visual Studio code can be used as IDE on Windows, MAC and Linux systems as well. .Net Core CLI can be used on the Command window to execute the .Net applications directly.
- dotnet new console
- dotnet build --output /build_output
- dotnet /build_output/my_app.dll
More about .Net Core 2.0
It's the latest of the Core versions now on the market. With the command line interface, it is just a few commands away from creating a .Net Core 2.0 application. .Net core 2.0 now comes with the Entity framework 2.0. To start with the .Net Core 2.0, one just needs to download and install the .Net core 2.0 SDK Creating a small and simple application using CLI
.Net Core 2.0 has a lot of performance improvements. One of the best improvements in .Net Core 2.0 is Razor Pages. Razor pages in .Net core 2.0 MVC projects are enabled by default using the services.AddMvc(); Now what's new in the integration of Razor Pages? In the Razor Pages now there is no direct coupling with the hard bound Controller we use to create and add views to them and then route through the controllers and Actions. Now, we can directly create a folder and add a .cshtml page. For example, we add a cshtml file named- "CoreTest.cshtml"
@page
Hello Core 2.0!
Here the routing would be "/CoreTest". Simple and crisp! The interesting thing is the previous page route was /CoreTest, now it can be customized and rerouted to the customized route to suppose "/core-test".
- services.AddMvc()
- .AddRazorPagesOptions((opts) =>
- {
- opts.Conventions.AddPageRoute("/CoreTest", "core-test");
- });
Razor compilation is easier and is automatically compiled during the publishing of the package or the application.
MvcRazorCompileOnPublish is the property that is set to true by default. We can set it to false to avoid this auto compilation.
The .Net core 2.0 simplified the Program.cs class file as compared to the .Net Core 1.x. For .Net Core 1.X
- public class Program {
- public static void Main(string[] args)
- {
- var host = new WebHostBuilder().UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseIISIntegration().UseStartup().UseApplicationInsights().Build();
- host.Run();
- }
- }
For .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();
- }
Now migrating a .Net Core 1.X project/application to .Net Core 2.0 is easy as well. We will cover the Migration Steps in the upcoming articles without letting down the scope of this article.
Lets create an MVC Web Application from Command Line
Using the command line interface we can create different types of dotnet applications without using the GUI. The commands to be used are,
- dotnet new -type
There are different types of dotnet applications that can be created using the new command and specifying the type. Earlier we created a console application, so the command was "dotnet new console". Similarly for a normal web project, the command would be "dotnet new web". For MVC applications, the command is "dotnet new mvc".
- dotnet restore
Restores the packages and assemblies while compiling the project. This is auto run during the new project creation process as well.
- dotnet build
This is the build command which runs before running the application required to compile the application.
- dotnet run
This command actually runs the appliction, hosting using the IIS and localhost port.
The entire command on CMD looks like below,
The project after creation looks like this:
The run command on CMD looks like this:
There are many more interesting features introduced in .Net Core 2.0. We will be covering the features in detail in the upcoming articles.