.Net Core .NET Core is a new version of .NET Framework, which is a free, open-source, general-purpose development platform maintained by Microsoft. It is a cross-platform framework that runs on Windows, macOS, and Linux operating systems. It can be used to build different types of applications such as mobile, desktop, web, cloud, IoT, machine learning, microservices, games, etc. .NET Core is written from scratch to make it a modular, lightweight, fast, and cross-platform Framework. It includes the core features that are required to run a basic .NET Core app. Other features are provided as NuGet packages, which you can add in your application as needed. .NET Core application speeds up the performance, reduces the memory footprint, and becomes easy to maintain. The .NET Core framework was named as .NET 5 after .NET Core 3.1. The main objective of .NET Core is to make .NET Framework open-source, cross-platform compatible that can be used in a wide variety of verticals, from the data center to touch-based devices. ASP.NET Core web applications can be deployed on multiple OS because ASP.NET Core web applications are self-hosted using an internal web server called Kestrel. The platform-specific web server such as IIS is used as an external webserver that sends requests to the internal webserver Kestrel.
.Net Core Version
Limitation with .Net Framework .NET Framework is designed for Windows-based systems only, while .NET Core supports cross-platform development, including Windows, Linux, and macOS. This makes .NET Core a better choice for developing modern web applications that can run on multiple platforms. Also, you need to use different .NET APIs for different Windows devices such as Windows Desktop, Windows Store, Windows Phone, and Web applications. the .NET Framework is a machine-wide framework. Any changes made to it affect all applications taking a dependency on it. .NET Framework is still a closed-source, Windows-only framework that is less flexible when it comes to development and deployment. .NET Core provides faster and more straightforward deployment with Docker, which simplifies the containerization process.
Advantages of Core over Framework Supports Multiple Platforms: can run on Windows, Linux, and Mac. IoC Container: includes the built-in IoC container for automatic dependency injection which makes it maintainable and testable. Integration with Modern UI Frameworks: such as AngularJS, ReactJS, Umber, Bootstrap, etc. Hosting: can be hosted on multiple platforms with any web server such as IIS, Apache etc. It is not dependent only on IIS as a standard .NET Framework. Code Sharing: It allows to build a class library that can be used with other .NET frameworks such as .NET Framework 4.x or Mono. Side-by-Side App Versioning: ASP.NET Core runs on .NET Core, which supports the simultaneous running of multiple versions of applications. Fast: ASP.NET Core no longer depends on System.Web.dll for browser-server communication. It allows us to include packages that we need for our application. This reduces the request pipeline and improves performance and scalability.
.Net Core Characteristics Open-source Framework Cross-platform Consistent across Architectures Wide range of Applications Supports Multiple Languages Modular Architecture CLI Tools Flexible Deployment Compatibility High Performance Unified Platform
Project Structure Analyzers are extensions for static code analysis. They help you to enforce coding standards, identify code quality issues, and detect potential problems in your code. Analyzers can be custom rules or third-party analyzers provided by NuGet packages. Frameworks node contains the target framework that your project is designed to run on. Properties It includes launchSettings.json file which includes Visual Studio profiles of debug settings. launchSettings.json helps developers to configure the debugging and launch profiles of their ASP.NET Core applications for different environments such as development, staging, production, etc.. wwwroot folder in the ASP.NET Core project is treated as a web root folder. Static files can be stored in any folder under the web root and accessed with a relative path to that root. All the css , JavaScript, and external library files should be stored here which are being referenced in the HTML file. appsettings.json file is a configuration file commonly used in .NET applications, including ASP.NET Core and ASP.NET 5/6, to store application-specific configuration settings and parameters. It allows developers to use JSON format for the configurations instead of code, which makes it easier to add or update settings without modifying the application's source code. program.cs This is an entry point of an application and sets up all necessary infrastructure for your application.
program.cs program.cs This is an entry point of an application. It contains logic to start the server and listen for the requests and also configure the application. Every ASP.NET Core web applications starts like a console application then turn into web application. When you press F5 and run the application, it starts the executing code in the Program.cs file. ASP.NET Core web application is a console application that builds and launches a web application. The program.cs file does not use the main() method, it uses the top-level statements so when the application starts, it starts executing code from top to bottom. Top-level statements in C# are a feature introduced in C# 9.0 to simplify the structure of your C# code by allowing you to write statements outside of traditional class and method definitions. With top-level statements, you can write C# code at the top level of a file without the need for a containing class or method. The first line CreateBuilder() method setup the internal web server which is Kestrel . It also specifies the content root and read application settings file appsettings.json. Using this builder object, you can configure various things for your web application, such as dependency injection, middleware, and hosting environment. You can pass additional configurations at runtime based on the runtime parameters. The builder object has the Services() method which can be used to add services to the dependency injection container. The AddControllersWithViews() is an extension method that register types needed for the application (like model, view, controller) to the dependency injection. It includes all the necessary services and configurations for MVC So that your application can use MVC architecture. The builder.Build() method returns the object of WebApplication using which you can configure the request pipeline using middleware and hosting environment that manages the execution of your web application. the method starts with "Use" word means it configures the middleware. app.run() method runs the application,start listening the incomming request. It turns a console application into a web application based on the provided configuration.
Startup class ASP.NET Core application must include 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 The name "Startup" is by ASP.NET Core convention. However, we can give any name to the Startup class. And call that like UseStartup < MyStartup >() Startup class includes two public methods: ConfigureServices and Configure. The Startup class must include a Configure method and can optionally include ConfigureService method . At run time, the ConfigureServices method is called before the Configure method. ConfigureServices () It is used to add services to the container and configure those services. service is a component that is intended for common consumption in the application. like MVC, EF core, send mail services The ConfigureServices method is a place where you can register your dependent classes with the built-in IoC container. After registering dependent class, it can be used anywhere in the application.
Startup class It includes built-in IoC container to provide dependent objects using constructors. ASP.NET Core refers dependent class as a Service. So, whenever you read "Service" then understand it as a class which is going to be used in some other class. ConfigureServices method includes IServiceCollection parameter to register services to the IoC container. Configure() Configure method is used to set up middleware. We manage HTTP request pipeline inside of the Configure method it is used to specify how the asp.net core application will respond to individual requests. can configure the application request pipeline for your application using the IApplicationBuilder instance that is provided by the built-in IoC container. ASP.NET Core introduced the middleware components to define a HTTP request pipeline, which will be executed on every request. You include only those middleware components which are required by your application and thus increase the performance of your application. UseDefaultFiles (): It configures the DefaultFiles middleware which is a part of StaticFiles middleware. This will automatically serve html file named default.html, default.htm, index.html or index.htm on the http request app.UseStaticFiles () method adds StaticFiles middleware into the request pipeline. The UseStaticFiles is an extension method included in the StaticFiles middleware so that we can easily configure it. Order of middleware is very important. app.UseDefaultFiles () should be added before app.UseStaticFiles () in the request pipeline.
.Net Core vs ASP.Net Core .NET Core is a general-purpose development platform that allows building applications for various domains, while ASP.NET Core is a web-specific framework built on top of .NET Core for developing web applications and APIs. .NET Core provides the foundation and runtime for running any .NET Core application, while ASP.NET Core extends .NET Core with web-specific features and tools. .NET Core is the foundation on which ASP.NET Core is built. It includes the common runtime, libraries, and tools that are essential for running any .NET Core application. ASP.NET Core, on the other hand, extends .NET Core by adding web-specific features and components, such as the MVC (Model-View-Controller) framework, routing, middleware, and web server integration. ASP.NET Core builds upon the infrastructure provided by .NET Core to enable web application development. .NET Core can be used for building web applications, but it does not provide the same level of web-specific features and capabilities as ASP.NET Core. ASP.NET Core offers a comprehensive set of tools and libraries for web development, including the ability to handle HTTP requests, manage routing, implement authentication and authorization, and generate HTML views using the MVC pattern. It also supports features like Razor Pages and SignalR for building modern web applications with real-time communication. .NET Core is designed to be cross-platform and can run on Windows, macOS, and Linux. It provides a consistent development experience across different operating systems. ASP.NET Core, being built on top of .NET Core, inherits cross-platform capabilities and can be used to develop web applications that can run on multiple platforms. This allows developers to target a wide range of deployment environments, including cloud platforms and containers. ASP.NET Core has a specialized ecosystem for web development. It provides additional libraries for HTTP requests, client-side scripting, integration with front-end frameworks, and database operations. ASP.NET Core also seamlessly integrates with popular development tools like Visual Studio and Visual Studio Code, enhancing the development and debugging experience for web applications.
.Net Core Application Type Portable Application: These are applications that expect .NET Core runtime on the deployment machines. It cannot be run on a machine that does not have .NET Core runtime installed. Self-contained Application: Self-contained applications are applications that include .NET Core runtime when we publish it. It can run on a machine that does not have .NET Core runtime installed. Application Type: We can configure ASP.NET Core application as portable or self-contained application using type property of Microsoft.NETCore.App dependency in project.json . The " type":"platform " indicates that this application expects .NET Core on the machine. For the self-contained application, remove type-platform from the dependency. This makes it a self-contained application which means .NET Core will be included when you build and publish an application.
Middleware A middleware is nothing but a component (class) which is executed on every request in ASP.NET Core application. In the classic ASP.NET, HttpHandlers and HttpModules were part of request pipeline. Middleware is similar to HttpHandlers and HttpModules where both needs to be configured and executed in each request. There will be multiple middleware in ASP.NET Core web application. It can be either framework provided middleware, added via NuGet or your own custom middleware. Every middleware will be executed in a sequence. To configure multiple middleware, use Use() extension method. It is similar to Run() method except that it includes next parameter to invoke next middleware in the sequence. The Run method adds a terminal middleware so it cannot call next middleware as it would be the last middleware in a sequence. Some Important middleware : Authentication, CORS, Session, Routing, StaticFiles , Diagnostics
Exception Handling By default, ASP.NET Core returns a simple status code for any exception that occurs in an application. ASP.NET Core includes a Diagnostics middleware that makes exception handling easy. Diagnostics middleware is used for reporting and handling exceptions and errors in ASP.NET Core, and diagnosing Entity Framework Core migrations errors Important method in Diagnostic Middleware: UseDeveloperExceptionPage : This extension method adds middleware into the request pipeline which displays developer friendly exception detail page. This helps developers in tracing errors that occur during development phase. UseExceptionHandler : This extension method allows us to configure custom error handling route. This is useful when an application runs under production environment.
Thanks for the Attention... Happy Coding... Follow me