ASP

Overview 

ASP.NET Core is the new version of the ASP.NET web framework mainly targeted to run on .NET Core platform. 

ASP.NET Core is a free, open-source, and cross-platform framework for building cloud-based applications, web apps, IoT apps, and mobile backends. It is designed to run on the cloud as well as on-premises. 

ASP.NET 3.x runs only on .NET Core 3.x, whereas ASP.NET Core 2.x runs on .NET Core 2.x as well as .NET Framework.

overviewASP

coreaspnet

Architecture

 

ASP.NET Core Solution Structure

Properties

The Properties folder contains a file called launchSettings.json. This json file contains all the project-specific settings needed to launch the application. You will find debug profile, environment variables that should be used etc in this file 

wwwroot

The wwwroot folder is new in ASP.NET Core.  The Static files like HTML, CSS, JavaScript, and image files go into this folder or any subfolder under it. 

The Code files should be placed outside of wwwroot. That includes all of your C# files, Razor files, configuration files, etc. 

Dependencies

This folder contains all the Dependencies of the project.

The Visual studio now uses NuGet Packages for all the server-side dependencies.

For the Client-Side dependencies, the Libman is used. This is a deviation from the previous versions where NuGet is used for both for Server & Client-side dependencies.

Program.cs

ASP.NET Core web application is actually a console project which starts executing from the entry point public static void Main() in Program class where we can create a host for the web application.

 

Startup.cs

It contains Startup class. It is like Global.asax in the traditional .NET application. As the name suggests, it is executed first when the application starts.

The startup class can be configured using UseStartup<T>() method at the time of configuring the host in the Main() method of Program class as shown below.

Fundamentals

Controllers 

Controllers are the brain of an ASP.NET Core application. They process incoming requests, perform operations on Model data and selects Views to be rendered to the user. Controllers are stored inside the Controllers folder in the root of the web application. They are basically C# classes whose Public methods are called as Action Methods. These Action Methods handles the HTTP request and prepares the response to be sent to the client. 

Actions 

Any public method on a controller type is an action. Parameters on actions are bound to request data and validated using model binding

Action methods should contain logic for mapping an incoming request to a business concern. Business concerns should typically be represented as services that your controller accesses through dependency injection. Actions then map the result of the business action to an application state. 

Actions can return anything, but frequently will return an instance of IActionResult (or Task<IActionResult> for async methods) that produces a response. The action method is responsible for choosing what kind of response; the action result does the responding.

Models

A model is a class that contains the business logic of the application. It also used for accessing data from the database. The model class does not handle directly input from the browser. It does not contain any HTML code as well. 

Models are also refers as objects that are used to implement conceptual logic for the application. A controller interacts with the model, access the data, perform the logic and pass that data to the view. 

Views 

ASP.NET Core View is a user interface which displays data from the Model to the user (in the browser) and also enables them to modify this data. They resides inside the Views folder located in the root of the application. 

This folder can contain a separate folder for each of the controller’s action method. Inside these folders separate views for these actions are placed. For example, the views of HomeController, resides in Views ➤ Home folder. In the same way, views which will be rendered from StudentController will resides in Views ➤ Student folder. 

URL Routing

ASP.NET Core URL Routing is used to create URL patterns for your application. It matches incoming HTTP requests and dispatching those requests to the application’s endpoints. Endpoints are those units that are responsible for request-handling. These are defined in the Configure() method of Startup.cs class. 

The Routing System has 2 main functions:

1. Maps the incoming URLs to Controllers and Actions.

2. Generates outgoing URLs so that a specific action method is called when the user clicks a link.

Web API 

ASP.NET Web API is an extensible framework for building HTTP based services that can be accessed in different applications on different platforms such as web, windows, mobile etc. It works more or less the same way as ASP.NET MVC web application except that it sends data as a response instead of html view. It is like a webservice or WCF service but the exception is that it only supports HTTP protocol. 

Deployment 

.NET Core provides 3 different types of deployment modes for accomplishing this task. 

Framework-dependent deployment (FDD) 

Framework-dependent deployment mode applications are completely dependent upon the .NET Core installed on the target machine where you want to deploy your app. The application only contains the app-specific code and external dependencies. 

Self-contained deployment (SCD) 

Self-contained deployment (SCD) does not use the common .NET Core install on the server. The final application build contains all the app-specific code and its dependencies along with .NET Core libraries and .NET Core runtime. In this mode, it is not necessary to have .NET Core runtime install on the servers. 

As .NET Core is cross-platform, we can build our code depending upon the platform we want to run the app. There are 6 different target platforms available: 

  • win-x86 

  • win-x64 

  • osx-x64 

  • linux-x64 

  • win-arm 

  • linux-arm 

Framework-dependent executables (FDE) 

It is exactly same as FDD but this mode produces the executable that can be run on any target machine. 

Core features 

The following are main features of ASP.NET Core: 

Cross-platform & container support  

With the introduction of .NET Core, you can now create ASP.NET applications and deploy them to Windows, Linux, and macOS. 

Containers like Docker, Kubernetes, and other technologies are all in high demand. Using ASP.NET Core, developers can utilize all the benefits provided by these new technologies. Microsoft Azure even has support for deploying our application to Containers and Kubernetes. 

High performance 

With the introduction of ASP.NET Core and the Kestrel web server, ASP.NET is touted as one of the fastest web application frameworks available. 

Kestrel is open-source (source code available on GitHub), event-driven, asynchronous I/O based server used to host ASP.NET applications on any platform. It’s a listening server and a command-line interface. You install the listening server on a Windows or Linux server and the command-line interface on your computer. 

It was launched by Microsoft along with ASP.NET Core. All ASP.NET Core apps utilize a new MVC framework and the Kestrel web server. 

ASP.NET Core applications to be run easily on other cross-platform webservers such as Nginx and Apache 

Asynchronous via async/await 

ASP.NET has excellent support for utilizing asynchronous programming patterns. Async is now implemented in all common .NET Framework classes and most third-party libraries. Most modern applications spend most of their time and CPU cycles waiting for database queries, web service calls, and other I/O operations to complete. 

One of the reasons ASP.NET Core is faster is its extensive use of asynchronous patterns within the new MVC and Kestrel frameworks. 

Unified MVC & Web API frameworks 

Before ASP.NET Core, developers were most commonly using the MVC and Web API frameworks. MVC was tailored to creating web applications that served up HTML. Web API was designed to create RESTful services using JSON or XML. 

With ASP.NET Core, MVC and Web API have been merged together. There was always a lot of overlap with the two frameworks. MVC could always return JSON data instead of HTML. 

Multiple environments and development mode 

The new environment feature in ASP.NET core allows us to easily differentiate between different behavior functionality exhibited in environments. This can be development, testing, staging or production. Before ASP.NET Core, there was no standard way of doing this. 

We can use the Startup.cs file to help configure our application for different environments. In different environments, we use different CSS or JavaScript files like we generally use CDN in production whereas local files during the development phase. 

 

Dependency Injection 

One of the great new features of ASP.NET Core is built in dependency injection. It is heavily used within ASP.NET MVC itself. It is the preferred way that things like logging contexts, database contexts, and other things are passed into your MVC controllers. ASP.NET Core injects objects of dependency classes through constructor or method by using built-in IoC container.

 

Web Sockets & SignalR 

ASP.NET has first class support for WebSockets. This can be used to persist long running connections and communicate back and forth with the browser. SignalR is a full framework that is also available that makes it easy handle common scenarios. 

We use SignalR very heavily at Stackify. For example, when viewing the current monitoring data about one of your servers, every time we receive new data, we immediately push it to your browser so you can see it update in real time. These types of scenarios are perfect for WebSockets and SignalR makes it easy to do. 

 

Cross-Site Request Forgery (CSRF) Protection 

Cross-Site Request Forgery (CSRF) is an attack that forces an end user to execute unwanted actions on a web application in which they’re currently authenticated. CSRF attacks specifically target state-changing requests, not theft of data, since the attacker has no way to see the response to the forged request. 

Core provides a very good framework that will be used to prevent these types of attacks. It generates anti-forgery tokens

“Self hosted” Web Applications 

Many times, we need to deploy web application on the desktop directly, not on the server which has IIS. To handle these kinds of scenarios, we can make use of the self-hosting feature of ASP.NET Core. 

There are many ways available to develop and deploy a self-hosted ASP.NET web application. In .NET framework 4.5, we can do this by using Owin, Nancy, or WCF. However, if we are using ASP.NET Core, we don’t need to depend on these features, we can directly use the standard Kestrel web server. 

One of the great advantages of using .NET Core is that web applications are basically just a console application. IIS just sits in front of it as a reverse proxy. To handle non-server based use cases, we can just deploy our app only with Kestrel. 

Action Filters 

One of the great features of ASP.NET is the support for extensible filters. This allows you to implement functionality that can be applied to an entire controller or action without modifying the action itself. 

Filters are used to specify caching, error handling, authorization, or any custom logic you would like to implement. 

Ex - OutputCache – This action filter caches the output of a controller action for a specified amount of time. 

 

Extensible Output Caching 

Output Caching enables the developers to store the HTTP response and page output in memory. Using Output Caching Feature ASP.NET can serve the subsequent requests more quickly by retrieving the data from memory. The limitation for this approach is It has to store the content always in memory 

ASP.NET 4.0 having an extensibility option for output caching by configuring custom output-cache providers.  Output-cache providers can use any storage mechanism to persist HTML content. These can include local or remote disks, cloud storage and distributed cache engines. 

Globalization and Localization 

ASP.NET makes it easy to localize dates, numbers, and the text within our web application. In case we want our application to be used across the globe, Localization will be the answer. 

Using resource files, we can enable the customization in our application to give multi-language support. We can consider these resource files as central repository where all the text for multiple languages are kept. The application can just read this resource file to populate the labels based on different regions. 

There are two types of resources: 

Local Resources: specific for a page (i.e., there will be local resource file for every page) 
Global Resources: common for the whole website (i.e., one resource file accessed by all pages) 

Swagger OpenAPI 

If you are creating API applications, you want to make sure you are using Swagger. It makes it easy to document and test your APIs. Swagger is a set of open-source tools built around the OpenAPI Specification that can help you design, build, document and consume REST APIs. 

ASP.NET has historically provided built-in functionality that is pretty similar for SOAP web services created with WCF. If you are using Web API or MVC for RESTful APIs, you definitely want to use Swagger. 

What to learn 

  1. .NET Core SDK 
  2. Configuration 
  3. Razor 
  4. Tag Helpers 
  5. Model Binding 
  6. Model Validation 
  7. Filters 
  8. Globalization & Localization 
  9. Identity 
  10. ADO.NET 

Leave a Reply