ASP.NET Core Tutorial

In this course of ASP.NET Core, we will cover all the basic, intermediate, and advanced ASP.NET core concepts that help you build data-driven web applications by the end of this course you will be able to perform all the CRUD operations that are Create, Read, Update and Delete using Sequal server as our database. 

The topics which we are going to cover in detail are-

  • ASP.NET Core.
  • ASP.NET Core MVC.
  • ASP.NET Core Identity.
  • Entity Framework Core.

What is ASP.NET Core?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, Internet-connected applications. ASP.NET Core is a redesign of ASP.NET 4.x and earlier it was named ASP.NET 5 and later is renamed ASP.NET Core.

Few benefits and features of ASP.NET Core.

Cross-Platform: ASP.NET Core applications can be developed and run across different platforms like Windows, macOS, Linux. ASP.NET Core applications can be hosted on IIS, Apache, Docker, and even Self-host in your own process.

Because of One Unified Programming Model for MVC and Web API, both the MVC Controller class and the ASP.NET Web API Controller class inherit from the same Controller base class and returns IActionResult.

ASP.NET Core has great build-in support for Dependency Injection which is a very great feature to learn.

ASP.NET Core Testability feature makes unit testing easy for developers.

It is an Open Source technology so it is continuously growing and improving with the support of developers from the open-source community.

Modular: ASP.NET Core provides modularity with Middleware Components. Both the request and response pipelines are composed using the middleware components. A rich set of built-in middleware components are provided out of the box. Custom Middleware Components can also be created.

Middleware - Is a piece of software that can handle HTTP request or response

Prerequisites for this course:

  • Basic understanding of HTML, CSS, and C#.
  • Prior MVC knowledge is helpful but not required. 

ASP.NET Core Project File.

.csproj or .vbproj depending on the programming language used.
No need to unload the project to edit the project file.
File or Folder references are no longer included in the project file.
The File System determines what files and folders belong to the project.

TargetFramework.
Specifies the target framework for the application.
To specify a target framework we used Target Framework Moniker (TFM)

AspNetCoreHostingModel.
Specifies how the application should be hosted.
InProcess or OutofProcess.
InProcess hosts the app inside of the IIS working process (w3wp.exe)
OutProcess hosting model forward web requests to a backend ASP.NET Core app running the Kestrel server. The default is OutofProcess hosting.

PackageReference.
Used to Include a reference to the NuGet package that is installed for the application.
Merapackage - Microsoft.AspNetCore.App
A metapackage has no content of its own. It just contains a list of dependencies (other packages).
When the version is not specified, an implicit version is specified by the SDK. Rely on the implicit version rather than explicitly setting the version number on the package reference. 

Main method in ASP.NET Core.
A Console application usually has a Main() method.
Why do we have a Main() method in ASP.NET Core Web Application?
ASP.NET Core application initially starts as a Console application.
This Main() method configures ASP.NET Core and starts it and at that point it becomes an ASP.NET Core web application.


The Main() method called "CreateWebHostBuilder" and we are passing the command line argument to it. We can notice that "
CreateWebHostBuilder" return "IWebHostBuilder" and "Build()" is called on that which build the webhost on which ASP.NET Core Application and on this we add "Run()" and start listening the incoming Http request.

We are also configuring the "Startup" class using the extension method named ".UseStartup"
Going to the definition of "Startup" class we find two important method "ConfigureService" configure the service required for our application and "Configure" configure our application request processing pipeline 

Some of the Tasks that CreateDefaultBuilder() performs.
Setting up the web server. Loading the host and application configuration from various configuration sources and Configuring logging.

An ASP.NET Core application can be hosted.
  • InProcess or 
  • OutofProcess.

ASP.NET Core InProcess Hosting.

To configure InProcess hosting.
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>

CreateDefaultBuilder() method calls UseIIS() method and host the app inside of the IIS worker process (w3wp.exe or iisexpress.exe)

InProcess hosting delivers significantly higher request throughput than OutOfProcess hosting 

To get the process name executing the app.
System.Diagnostics.Process.GetCurrentProcess().ProcessName

With out-of-process hosting.
2 Web Servers - Internal and External Web Server.
The internal web server is Kestrel.
The external web server can be IIS, Nginx, or Apache.

What is Kestrel?
Cross-Platform Web Server for ASP.NET Core.
Kestrel can be used, by itself as an edge server.
The process used to host the app is dotnet.exe.


Dependency Injection in ASP.NET Core.

What is Dependency Injection?
Dependency Injection (DI) is the technique to achieve IOC (Inversion of Control). Inversion of Control means we need to make our code loosely coupled so in the future if there are any changes then there should not be any tight coupling between any two classes of object.


This is the basic structure of our project when we are not using Dependency Injection and we are using some service(class) inside one or more Controller then we create an object of the service using the "new" keyword and suppose in the future if we make any changes like changing the name of the service then we have to make those changes inside all Controllers. This is an example of tightly coupling.


Now let's understand what happens when we work with Dependency Injection, suppose I have only one service so to work with dependency injection I need to create one interface of my service. Now I will not work directly with the service I will use an interface to create this field and to store the object of this particular service. Now if we are making any changes in service then we don't have to make any changes in the control which is using it.

Now one important question that may come to our mind that where I will create the instance of the service and how the application will come to know that this interface is resolved by using this particular class. ???

In the concept of dependency injection, we generally create a container, and inside that container, we define some settings. 

How to configure Dependency Injection in ASP.NET Core?


ASP.NET Core provides the built-in support for DI. Dependencies are registered in containers and the container in ASP.NET core is IServiceProvider. Services are typically registered in the application's Startup.ConfigureService method.

Service Lifetime.
Service can be registered with following lifetimes-
  • Transient (AddTransient<>) - A  new instance of the service will be created every time it is requested.
  • Scoped (AddScoped<>) - These are created once per client request. For each HTTP request
  • Singleton (AddSingleton<>) - Same instance for the application.

Structure Query Language (SQL) | RDBMS

There are many DBMSs present in the market. Do we have to learn all the languages to manipulate the data? The answer to this question is NO! We don't have to learn all the languages. We have a common language named SQL (Structure Query Language). RDBMS also uses SQL to access the database. Users use many applications and those applications use SQL to interact with DBMS to manipulate the data. 
 
Structure Query Language

What is SQL (Structured Query Language)?

SQL is an ANSI (American National Standard Institute) standard computer language for accessing and manipulating database systems. SQL statements are used to retrieve and update the data in the database. They are not case-sensitive in nature. 

Some key points to write better SQL queries:
  • Use pascal notation for object name. Example: Products, Customers
  • Use the singular form of nouns for the Column name. Example: FirstName, Address
  • Each table must have a primary key. 
  • Use upper case for all SQL keywords. Example: SELECT, UPDATE, INSERT, DELETE
  • Do not use white space in identifiers.
  • Use parentheses to increase readability.
  • Indent code to improve readability.
  • Use ANSI joins instead of old-style joins.
  • Do not use SELECT *
  • Always use table aliases when your SQL statement involves more than one table.
  • Do not use column numbers in the ORDER BY clause.
  • Always use column list in INSERT statements.
These key points might be confusing for you because we have not started learning how to write SQL queries access and insert data in our database. But before moving to that, it is important for us to understand components of SQL

Components of SQL.

SQL is divided into four different components. Let us understand each of them one by one:

DDL (Data Definition Language).
Data Definition Language deals with the structure of Database objects and commands are CREATE, ALERT, TRUNCATE, DROP.
  • CREATE: used to create new Database objects like table, view, and stored procedure.
  • ALERT: used to modify the existing structure of the database objects.
  • TRUNCATE: used to remove all the data from database objects.
  • DROP: used to remove the database object from the database.
  • RENAME: used to change the name of the existing object.

DML (Data Manipulation Language).
Data Manipulation Language deals with the manipulation of the data in the Database objects and the commands are SELECT, INSERT, UPDATE.
  • INSERT: used to insert data into a table.
  • UPDATE: used to update existing data within a table.
  • DELETE: used to delete records from the table.

TCL (Transaction Control Language).
Transaction Control Language deals with transaction management and the commands are COMMIT, ROLLBACK.
  • COMMIT: used to end the current transaction by making all pending data changes permanent. 
  • ROLLBACK: used to ends the current transaction by discarding all pending data changes.
  • SAVEPOINT: used to mark a savepoint within the current transaction.

DCL (Data Control Language).
Data Control Language deals with providing access privilege of the data by using command GRANT, REVOKE.
  • GRANT: used to give user access privilege to the database.
  • REVOKE: used to revert back the user access privilege to the database.
DQL (Data Query Language).
DQL command is used to perform quarries to fetch the data from the database within the schema object. We can use the DQL command with JOIN to get the data from multiple tables at one time using Primary key and Foreign key relations. Example: SELECT

So these are the few important components of SQL and in our further articles, we are going to learn the syntax of all the SQL commands with their practical examples. 

Relational Data Model and Its Properties.

In a relational data model, the data is stored in the form of tables with rows and columns. There are a few important terminologies that we all should know before moving forward with the relational data model. 

Let's look at the terminologies used in Relational Data Model:
  • Relation refers to the table and Cardinality refers to the number of rows in the table.
  • Tuples refer to the row of the table.
  • Attributes refer to the column of the table.
  • Degree refers to the number of columns in the table.
  • Domain refers to the range of values that can be stored for an attribute.
Relational Data Model Terminologies

Now the most important thing to understand in Relational Data Model is how we are going to establish a relationship between one table with one or more other tables? The two keys which play a very important role in building a relationship are the primary key and the foreign key. Let us understand these two important terms in more detail.

To build a relation of one table with another table, the table must contain one column which contains a unique key for each row of the table called the primary key. This primary key column must be present in another table as a foreign key column. We can use different kinds of JOIN operations to get the data from more than one table. 
The primary key column cannot hold null values and there should be only one primary key column in each table. 
The foreign key column may contain null values and there can be more than one foreign key column in a single table.(alert-passed)
Primary Key Foreign Key

There are a few important properties of the Relational Data Model that we should kind in mind before we start working with the Relational Database.

  • No duplicate Tuples are allowed.
  • Tuples are unordered.
  • Attributes are unordered.
  • Attributes values are atomic.

Relational Database Management System.

A Relational Database Management System is system software that lets us Create, Update and Administer a Relational Database. RDBMS uses SQL (Structured Query Language) to access the database. The few best examples of RDBMS are Oracle, DB2, MYSQL, etc. 

We will learn more about RDBMS and SQL in our next article. You can add your valuable feedback in the comment section below.

RDBMS Introduction | RDBMS Tutorial

RDBMS Intro

RDBMS stands for Relational Database Management System. It is a system that is used by software to store, manage, query, or retrieve data from the database in which data is stored in the form of tables and one table might be connected with many other tables using primary and foreign key relations. RDBMS provides us the interface between software and database to manage and perform the required operations on the database. Before starting with RDBMS concepts, it is important to understand a few important terms.

What is Information System?

Data with its meaning is referred to as information, where the data means raw fact. In specific, an information system is an organized collection of hardware, software, supplies, and procedures and people who store, process and provide access to information.

What is File Based System?

When information is stored in flat files, which are maintained by the file system under the operating system control. Application programs go through the file system in order to access these flat files. Records consist of various fields, which are delimited by a space, comma, pipe, or any special character, etc.

Maintaining records in a File based system is great and we are able to free up all that space by moving all the data on the computer.(alert-success) 

There are many disadvantages of using the traditional file-based system like:

  • The application develops in an ad-hoc manner.
  • Data redundancy, because data can be duplicated in two or more files.
  • Data isolation, which means all the related data are scattered in various files having a different file format, and hence, writing a new application becomes difficult in retrieving data.
  • In File Based System it is difficult to produce reports across sales, product, and customer data because they are maintained on a separate file system. (alert-error)

What is Database?

A database is a shared collection of logically related data and the description of this data, designed to meet the needs of an organization. 

Advantages of Database Approach:

  • Centralization of Information Management.
  • Data is shared by different groups of users and application programs.
  • Representation of complex relationship between data.
  • Integrity Constraint handling.
  • Advanced facility for backup and recovery.

What is Database Management System?

Database Management System (DBMS) is software that helps in defining, creating, and maintaining the database that provides controlled access to the database.

Advantages of Database Management System:

  • Shared file system.
  • Enforcement of Security. 
  • Enforcement of development and maintaining standards.
  • Reduction of redundancy.
  • Avoidance of inconsistency across files.
  • Maintenance of integrity.
  • Data Independence. 
  • Authentication- Whether the right user has the right to access the database.
  • Authorization- Whether the right user has the right to access the database.
DBMS System

We can categorize database users as follows:

Application programmers or Ordinary users: Developer who writes application programs to interact with the database. Application programs can be written in many programming languages like C++, JAVA, C#, or any high-level programming language. Such a program access the database by issuing the appropriate request like SQL statement to DBMS.

Sophisticated users: Users who interact with the system by forming their requests in a database query language. Each such query is submitted to a query processor whose function is to break down the DML statement into instructions that the storage management understands.

End Users: Users who interact with the system by invoking one of the permanent application programs that have been written previously. 
 
DBA(Database Administer): User who manages the database like installation of DB, managing user and DB performance. 

Data Model.

A Data Model is a way of explaining the logical layout of the data and the relationship of various parts to each other on the whole. Different types of Data Models are:
  • Hierarchical models refer to storing the data by a tree structure. This model handles only parent-child relationships which are one-to-many relationships. It is not easy to perform, insert, update and delete operations in this model.
  • In-Network models data is represented as a graph which Nodes and Edges. It addresses many to many relationships. It has a very complex design.
  • Relational Data Model is a widely used data model. The data is stored in the form of tables with rows and columns and it is easy to use because there is no usage of a pointer. Data access is faster than other models. This data model uses the relational algebra concept. 
So this was the basic introduction of database and its type and why do we need a Relational Database Management System. You can add more value to this post by giving your valuable feedback in the comment section below.

How a DotNet Program Execute ?

To understand how a DotNet program execution takes place we have to do the comparison between pre-Dotnet program execution with DotNet program execution. So before DotNet came into existence, we used to build the application using other programming languages like C++ or VB6. 

Let's, for example, build an application using VB6 and compile it using a language compiler then an assembly gets generated with .dll or .exe extension depending on the type of application. This assembly is in Native code or Machine code format. We have to do the compilation because our operating system doesn't understand high-level programming languages, they only understand binary code of 0s and 1s. So basically we deploy that assembly onto the system to get executed which is nothing but a native code. 

Now lets us understand what naive code means? Suppose you have Windows operating system and now you take the assembly which was compiled on the Windows operating system and try to run it on Linux or another operating system then it will give you an error because that code is native to only Windows operating system. That's why it is called Native code or Object code. This is the problem with the pre-DotNet application, they are not portable and we have to build different operating systems.

Now let's understand how a DotNet application executes. DotNet supports several programming languages like C#, VB, C++, etc. When you build a DotNet application using any of these languages and compile using the respective language compiler, we get an assembly here also but unlike pre-DotNet application, it does not contain native code, instead, it contains intermediate language. This is the main difference between a DotNet program and a Pre-DotNet program but your operating system cannot understand the intermediate language so we need a runtime environment name as CLR (Common Language Runtime) to convert the intermediate language to native code. 

Now the question is how does this CLR come in between to do this job? When we install DotNet on our system, we basically install two important components one is the DotNet framework class library and another one is CLR. This is like a heart for DotNet program execution. This CLR layer exists between Assembly and the Operating system. Within CLR there is a very important component named as JIT compiler which takes IL (Intermediate Language) as input and generates native code as the output for the Operating system. 

So the biggest advantage of the DotNet application is that is portable which means that the same code can run on any operating system with the help of CLR installed on that operating system. Another advantage of DotNet is that the runtime environment contains a garbage collector which will clean the object or memory that is not in use programmer doesn't have to worry much about memory management.

Note: The native code is not stored permanently anywhere after we close the program the native code is thrown away and when we execute the program again the native code will generate again. (alert-success)


 

ASP.NET Life Cycle.

ASP.NET is a server-side technology used for developing dynamic websites and web applications. It is the latest version of active server pages, active server pages is a server-side scripting language and engine used for developing web applications. It is essentially a web-based platform that helps to create a dynamic web-based platform that helps to create dynamic web pages for programmers. 

ASP.NET has some specific steps that are to be carried out for an application these steps are called Life Cycle.

ASP.NET Life Cycle is divided into two groups:

1. Application Life Cycle.

2. Page Life Cycle.

Application Life Cycle.

Application Start > Object Creation > HTTP Application > Dispose > Application End

  • Application start is a method that can be executed by the webserver when a user makes a request. 
  • The HTTP requests contain all the information about the current request, like cookies and browser information. 
  • An object helps to process each subsequent request sent to the application that is created by the web browser. 
  • Dispose is an event that can be called before the application is destroyed. This helps to release manually unmanaged resources. 
  • Application end is the final part of the application and this can help to unload the memory. 

Page Life Cycle.

ASP.NET web page life cycle has specific steps that are carried out during the execution. These phases include initialization, restoring, and execution. The important phases of the page life cycle are: 
  • Page Request: When the user requests a page, the server checks the request and then compiles the page and responds to the user. If the page is requested several times then the cache will check the request to see if the output exists or not and after that it will send a response back to the user. 
  • Page Start: In this phase, two steps are carried out request and response. The request holds all information that was sent when the page was requested and the response holds all the information which send back to the user. 
  • Page Initialization: In this phase, all the controls on the page are set and each has a particular ID, and themes are applied to the pages. 
  • Page Load: In this phase, all the controls properties are loaded, and information is set using view state and control state. 
  • Validation: Validation happens when the page execution goes successful and then it returns true else the execution fails and it returns false. 
  • Event Handling: Event handling happens in response to validation. In this case, the page is loaded again and displays the same information so the postback event handler is called in order to overcome this problem. This event helps in checking the credentials of the user.
  • Rendering: Rendering occurs before all the response information is sent to the user. It also stores all the information sent to the user. 
  • Unload: Unload phase helps to clean all the unwanted information, it also cleans the memory once the output is sent to the user. 

What is ASP.NET? Introduction to ASP.NET



Many questions come to our mind while building an application and all we think about is the speed, cost, and language. And ASP.NET is one of the best choices for building any application. 

Why use ASP.NET?

For building Web application ASP.NET is much easier compared to other technology like PHP and Node.js. It is much faster much efficient than many other languages. ASP.NET allows you to build many applications like REST API and Web pages. It takes less time for execution and execution takes place only once. 

What is ASP.NET?

ASP.NET is server-side technology used for developing dynamic Web applications. It was released in 2022 and had an extension of .aspx. It produces interactive, data-driven Web applications on the internet. It is developed by Microsoft and it is mainly used to develop websites and web applications. ASP.NET is the latest version of active server pages which Microsoft developers to build a website. 

The architecture of ASP.NET.

ASP.NET architecture help in increasing performance, stability, and flexibility. ASP.NET works on HTTP protocol and uses HTTP commands. First, ASP.NET request an HTML file from the server then IIS (Internet Information Services) passes that information to the ASP.NET engine, and after that ASP.NET engine read the file and execute the script. Then ASP.NET returns to the browser as an HTML file. 

Note: IIS is a powerful web server that provides a very reliable, scalable,  and manageable Web application Infrastructure. It lowers the system administration costs, which helps organizations to increase Website and application availability. 

Components of ASP.NET.

  • Language: This can help in developing the web application. Some of them are VB.net and C#.
  • Library: It is a prewritten class and coded template that a developer can use while developing an application. The most common library used in ASP.NET is the web library. 
  • Common Language Runtime: This is the platform that is used to execute the program. It is also use for performing activities like Garbage Collection. 
Skills required to Master ASP.NET.
  • Basic of C#, JavaScript, HTML.
  • Knownledge of ASP.NET MVC.
  • Good Knowledge of OOPs
  • Web Development.
Benefits of ASP.NET.
  • High Performance. 
  • Secure
  • Multiple Development modes.
  • Language Independent.
  • Globalization and Localization. 

What is Entity Framework ?

Entity Framework is an ORM framework. ORM stands for Object Relational Mapping. Object Relational Mapping automatically creates classes based on database tables, and vice-versa is also true, it means it can generate necessary SQL to create database tables based on classes. .Net applications may connect with any kind of data sources like flat files, XML data, or relational databases. Irrespective


If you have two tables showing on the left side and you want to create a new table using that data then you have to follow the following steps in ADO.NET:

1. We need to create Department and Employee classes. 

2. Write ADO.NET code to retrieve data from the database. 

3. Once the database is created, we need to create department and employee objects and populate them with the data.

Note: Entity Framework can do all the above automatically if we provide it with the database schema. 

Entity Framework provides us a class called DbContext which is our gateway to our database. A DbConext can have one or more DbSet and this DbSet represent a table in our database. We use LINQ query to query this DbSet and Entity Framework translates these LINQ queries to SQL queries at runtime. 

Three Important Entity Framework Approaches are: 

1. Database First Approach.

  • We design our tables.
  • Entity Framework generates domain classes.

2. Code First Approach.

  • We create our domain classes.
  • Entity Framework generates database tables.

3. Model First Approach.

  • We create a UML diagram.
  • Entity Framework generates domain classes and databases. 

DON'T MISS

Nature, Health, Fitness
© all rights reserved
made with by templateszoo