In this article, we will learn about .Net Core Features & short overview on .Net Framework (existing), we will explore what changes made/added in new environment of .Net Core.

After that we are also going to explore ASP .NET Core Initial Template one by one, then perform some basic operations (CRUD) with data using MVC Core & Entity Framework Core. 

Topics to be discussed:

  1. Short Overview on .Net Framework
  2. What is .Net Core?
  3. What is ASP.Net Core?
  4. How to start?
  5. Detail Overview on initial sample application
    1. Global.json
    2. Properties
    3. wwwroot
    4. Controller
    5. Views
    6. appsettings.json
    7. bower.json
    8. bundleconfig.json
    9. program.cs
    10. project.json
    11. startup.cs
    12. web.config
  6. Working with data using
    1. ASP.Net Core &
    2. Entity Framework-7 (Core)
    3. MVC-6 (Core)

 

Ok, let’s start with our topic one by one to be clarified our thought on magical world of .Net.

.Net Framework:

The .NET Framework (pronounced dot net) is a software development platform developed by Microsoft. Provides tools and libraries that allow developers to develop applications.

Microsoft started development on the .NET Framework in the late 1990s originally under the name of Next Generation Windows Services (NGWS). By late 2001 the first beta versions of .NET 1.0 were released.

.Net Framework Releases:

Here I have provided a short overview on .Net Framework Version evaluation that all we knew about, but it was just recapping for better understand of .Net thinking & road-map.

core_1

So fur we have known all those releases, but what about the new announcement? Is .Net Core another framework? Ok, let’s just be easy, Step by step we will uncover all those issues.

.Net Today: Microsoft has announced a lots of exciting new innovations that is including with .Net in this year, among them the most power full is open source cross-platform development (Any developer, Any app, Any platform). Learn more

core_2

 .Net Core:

Simply .NET Core is modern, lightweight, high performance modular platform for creating web apps and RESTful APIs that run on Cross-Platform (Windows, Linux and Mac).

It is smaller set/sub-set of .Net Framework (Full), that maintained by Microsoft & the .NET community on GitHub.

.Net Core Facilities:

  • Cross-platform that runs on Windows, macOS and Linux.
  • Open source.
  • Command-line tools that can be exercised at the command-line.
  • Compatible with .NET Framework, Xamarin and Mono, via the.NET Standard Library.
  • Flexible deployment.

Learn more on .Net Core …

 .NET Core Platform:

The .NET Core platform is made of several components

  1. CoreFX – .NET Core foundational libraries
  2. CoreCLR – .NET Core runtime
  3. CLI – .NET Core command-line tools
  4. Roslyn – .NET Compiler Platform

 

Diagram of .Net Core vs .Net Framwork

core_3

.Net Core Releases:

  1. .NET Core 1.1 released on 11/16/2016
  2. .NET Core 1.0 Preview 1 released on 10/24/2016
  3. .NET Core 0.1 released on 9/13/2016
  4. .NET Core 0.0 released on 6/27/2016
  5. .NET Core RC2(Release Candidate) released on 5/16/2016
  6. .NET Core RC1(Release Candidate) released on 11/18/2015

Download the latest .Net Core SDK (Runtime + Command line Tool) or Only Runtime.

.NET Core supports: Initially .NET core supports four types of application development, among them we will focus on ASP.Net Core application development.

  1. NET Core web apps,
  2. Command-line apps,
  3. Libraries, and
  4. Universal Windows Platform (UWP) apps.

ASP.Net Core:

ASP.Net Core is re-written new open source ASP.Net web framework for building web based application (web apps, IoT apps & mobile backends) that run on both full .Net Framework and .Net Core.

ASP.Net Core Facilities:

  1. Developed and run on Cross Platform (Windows, Mac and Linux).
  2. Open Source.
  3. Built on the .NET Core runtime & also on .NET Framework.
  4. Facility of dynamic compilation.
  5. Built in Dependency Injection (DI).
  6. MVC & Web Api Controller are unified, Inherited from same base class.
  7. New light-weight and modular HTTP request pipeline
  8. Ability to host on IIS or self-host in own process.
  9. Ships entirely as NuGet packages
  10. Smart tooling like (Bower, Grunt & Gulp).

Learn more on ASP.Net Core

How to start?

Here are the quick installation guide for Windows.

Steps:

  1. Install Visual Studio 2015 (provides a full-featured development environment).
  2. Make sure you have Visual Studio 2015 Update 3
  3. Install the .NET Core tools preview for Visual Studio.
  4. Create a new .NET Core project
  5. Run the application

Learn more on how to get started…

Anatomy of ASP.Net Core Initial Application:

Global.json

It is a JSON schema for the ASP.NET global configuration files, Contains the Solution information/metadata. In this file we can configure the sdk with version, architecture or runtime that specifies which processor architecture to target. Which runtime to target.

Let’s get the properties in details

  1. Projects: Specify the folders to search that contain the project.
  2. Packages: Specify the package location.
  3. Sdk: Specify information about the SDK.

Here is our global.json properties by default.

To get other’s properties information details go to > http://json.schemastore.org/global

Properties > launchSettings.json

JSON schema for the ASP.NET DebugSettings.json files.

This is where we can configure our debug profile by using IDE interface, another way to set properties is launchSetting.json file.

Right Click on Project Properties > Debug.

core_4

Here’s the configuration properties by default in launchSetting.json

We can add another profile as production to the json file using same command as IISExpress. Based on this environment profile we can show hide html tags on different environment mood, which is known as working with multi environment, later we will discuss on it.

The new debug profile will appear like below image.

core_5

Let’s get the properties in details

  1. commandLineArgs: The arguments to pass to the command.
  2. workingDirectory: Sets the working directory of the command.
  3. launchBrowser: Set to true if the browser should be launched.
  4. launchUrl: The relative URL to launch in the browser.
  5. environmentVariables: Set the environment variables as key/value pairs.
  6. sdkVersion: Sets the version of the SDK.

To get other’s properties information details go to > http://json.schemastore.org/launchsettings

wwwroot

It’s all about serving static file directly to client. We need to serve static file all we need to add extension method UseStaticFiles() in startup class Configure method.

core_6

Then resolve the dependency package “Microsoft.AspNetCore.StaticFiles”: “1.0.0” for static file in project.json.

core_7

Controller

ASP.Net Core the Controllers are now unified, there’s no differences between MVC Controllers (Controller base class) & WebAPI Controllers (ApiController base class).

As you can see I have put it side by side to show the difference between both MVC & WebAPI Controller, both controller is being inherited from same controller.

core_8

Views

ASP.Net Core MVC views are .cshtml files as we know earlier version of ASP.Net MVC views that use the Razor view engine to render views. Here I have showed MVC folder structure and the related .cshtml files in our ASP.Net Core application.

core_9

Learn more about views & tag-helpers……

appsettings.json

This is the application configuration file that keep the configuration value of Key/Value pair. Previously this configuration were stored in web.config file.

We can add the connection string to the appsetting file and then we have accessed the connection in startup.

In startup file we added our service in ConfigureService method to enable the database connectivity through connection string.

Here GetConnectionString is an extension method that passing the connection name “dbConn”.

bower.json

Bower is a Package manager the automatically add/update client side packages (like bootstrap, jquery etc) while we add/remove listed packages in json file.

Here is the listed Client-side dependencies, there is another type of dependencies called Server-side dependencies.

Here private: true means it will refuse to publish, prevent accidental publication of private repositories.

core_10

To change bower installation location open .bowerrc and change the directory value.

core_11

Here you can see the dependencies have installed in wwwroot/lib folder as initial value.

core_12

bundleconfig.json

Bundling and minifying JavaScript, CSS and HTML files in any project. Download BundlerMinifierVsix & install in visual studio 2015. Restart the IDE.

core_13

Task Runner Explorer will appear, to update all right click > Update all files > Run

core_14

Finally the output file will generated.

core_15

Here is a bundleconfig.json look like:

program.cs

This is the main point of any ASP.Net core application that prepare the host to run all configured services. Here is the program.cs code snippet.

In program.cs class simply do the job of configuring & launching a host using WebHostBuilder. As we know .Net Core application are Console Application that need to execute by host. WebHostBuilder is creating the host to bootstrap the server by UseKestrel() extension method, this means Kestrel server is going to host the application. There is another extension method UseIISIntegration() that is for IIS Server.

Here’s a diagram that clarify the process between IIS and ASP.NET Core applications.

core_16

Source: ASP.NET Core Module overview By Tom Dykstra, Rick Strahl, and Chris Ross

The WebHostBuilder is responsible for creating the host that will bootstrap the server for the app. learn more about hosting…

We can say that .Net Core Application need a host to launch that define which server is going to use, where to get the content files and specify the startup (middleware) services.

project.json

The project.json file store the application information, dependencies, and compiler settings. It’ has several section like Dependencies, Tools, Frameworks, Build Options and much more.

Here I have shown how the default project.json content looks like:

Let’s explorer the different section in it step by step:

project.json > Dependencies:

This section manages the project dependencies using key/value pair, we can add new dependencies if required, intellisense will help up to include with name & version.

core_17

As you can see from the upper image I was adding three new packages for database service, intellisense was helping me by showing with the version of that package. While I saved the changes it was automatically restoring the required dependencies from NuGet.

core_18

project.json > Tools:

This section manage & list command line tools, we can see IISIntegration. Tools is added by default which is a tools that contain dotnet publish iis command for publishing the application on IIS.

EntityFrameworkCore Tools (Command line tool for EF Core) Includes Commands

For Package Manager Console:

  • Scaffold-DbContext,
  • Add-Migration,
  • Udate-Database

For Command Window:

  • dotnet ef dbcontext scaffold

Both EF command shows here please have a close look on that.

If we disable the EntityFrameworkCore Tools and run the command it will show’s below error in Package Manager Console:

core_19

project.json > Frameworks:

Define the framework that support in this application, we can use multiple framework by configuring in this section.

Let’s clarify a bit more, in the framework section we are targeting netcoreapp1.0 framework. By configuring import section we can use several targeted packages/class libraries that are different from our application target version.

Here “dotnet5.6” for older .NET Core preview versions, and “portable-net45+win8” for portable class library (PCL) profiles, that known as Target Framework Monikers (TFMs).

We can use multiple framework by adding “net461”: {} in framework section.

You may notice that we have move our Microsoft.NETCore.App dependency from global dependencies to specific framework dependency section.

core_20

This is how we are using multiple frameworks/runtimes that support.

project.json > Build Options:

Options that are passed to compiler while build application.

project.json > RuntimeOptions:

Manage server garbage collection at application runtime.

project.json > PublishOptions:

This define the file/folder to include/exclude to/from the output folder while publish the application.

project.json > Scripts:

Scripts is object type which specifies that scripts to run during build or publish the application.

Learn more about project.json file…….

startup.cs

This is the entry point of every ASP.Net Core application, provide services that application required.

Here’s a diagram that show what happened at runtime while first-time it’s run.

core_21

In Startup class there are several method with different responsibilities. Here I have separated them, the very first is the constructor of Startup class. In the constructor IHostingEnvironment instance is passed as parameter to get the web hosting environment details.

core_22

The instance of ConfigurationBuilder call the extension methods of multiple configuration source then chain calls together by order by as a fluent API.

core_23

public IConfigurationRoot Configuration { get; }

The IConfigurationRoot force the configuration values to be reloaded while application is running & configuration data is changed.

Next method is ConfigureServices that is get called before Configure method. This section we can configure our required service to use at runtime in our application. Instance of IServiceCollection has passed as parameter. IServiceCollection Specifies collection of different services.

In Startup class the Configure() mothod handle the HTTP request .

The full startup look like this:

web.config

In ASP.Net Core the application configuration is now store in appsettings.json file, previously this was stored in web.config file. Here is the default web.config file look like:

Web.config is using only for configuration while hosting in IIS. In handler section, this is where IIS handle a request by passing it to ASP.Net Core Module.

We can specify environment through web.config in IIS.

Working with Data Using ASP.Net Core MVC (CRUD)

Database Creation

Create a new database using SSMS, name it “TicketBooking”. Copy the below query & run it using query editor of SSMS.

Reverse Engineer:

Add Dependency Packages

Add Tools

Packages will automatically restored by IDE

core_25

Create the EF model from existing database.

  1. Tools –> NuGet Package Manager –> Package Manager Console
  2. Run the following command.

core_26

As you can see models has created in Models folder. Rewrite Entity from Database with below Command if database has any changes.

Context

MVC Controller

WebAPI Controller

 Output:

core_27

Source Code: I’ve uploaded the full source code to download/clone @github, Hope this will help 🙂

References:

  1. https://en.wikipedia.org/wiki/.NET_Framework
  2. https://en.wikipedia.org/wiki/.NET_Framework_version_history
  3. https://dotnet.github.io
  4. https://docs.microsoft.com/en-us/aspnet/core
  5. https://github.com/aspnet
  6. https://www.microsoft.com/net/core/platform
  7. https://github.com/dotnet/core
  8. https://docs.microsoft.com/en-us/dotnet/articles/core/index
  9. http://www.codeproject.com/Articles/1115771/NET-CORE-MVC-ANGULARJS-STARTUP
  10. http://www.codeproject.com/Articles/1118189/CRUD-USING-NET-CORE-ANGULARJS-WEBAPI
  11. https://docs.microsoft.com/en-us/aspnet/core

 

Leave a Reply

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