Asp .Net Core
Global exception handler:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-2.2
Asp.net core hosting Issue:
https://blogs.msdn.microsoft.com/benjaminperkins/2018/06/07/asp-net-core-2-1-and-http-error-502-5-process-failure/
ASP.NET Core is a modular framework distributed as NuGet packages. This allows us to include packages that are required in our application.
ASP.NET Core is designed to be deployed on cloud as well as on-premises. Developers can now build cloud-based web applications, IoT (Internet of Thing) and mobile backend applications using ASP.NET Core framework which can run on Windows, Linux, and Mac operating systems.
NET Core is a fast, lightweight, modular and open source framework for creating web applications and services that run on Windows, Linux and Mac. So, it is a platform on which ASP.NET Core application runs.
.NET Core is named "Core" because it includes core features of the .NET framework. The main objective of .NET Core is to make .NET framework open source, and cross-platform compatible so that it can be used in resource-constrained environments. It includes minimum features that are required to run a basic .NET Core app and other advanced features that can be included as a package from NuGet.
Dependencies
And lastly, the
IApplication
launchSettings.json file : change the
Program.cs :Method
Startup.cs:In the
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-2.2
Asp.net core hosting Issue:
https://blogs.msdn.microsoft.com/benjaminperkins/2018/06/07/asp-net-core-2-1-and-http-error-502-5-process-failure/
ASP.NET Core
Referehttps://www.tutorialsteacher.com/core/aspnet-core-introductionnces:
ASP.NET Core is a free, open-source and cloud optimized web framework which can run on Windows, Linux, or Mac.
ASP.NET Core is a modular framework distributed as NuGet packages. This allows us to include packages that are required in our application.
ASP.NET Core is designed to be deployed on cloud as well as on-premises. Developers can now build cloud-based web applications, IoT (Internet of Thing) and mobile backend applications using ASP.NET Core framework which can run on Windows, Linux, and Mac operating systems.
Version | Release Date |
---|---|
ASP.NET Core 2.0 | August 2017 |
ASP.NET Core 1.1 | November 2016 |
ASP.NET Core 1.0 | June 2016 |
.NET Core
Please note that ASP.NET Core and .NET Core are not the same.NET Core is a fast, lightweight, modular and open source framework for creating web applications and services that run on Windows, Linux and Mac. So, it is a platform on which ASP.NET Core application runs.
.NET Core is named "Core" because it includes core features of the .NET framework. The main objective of .NET Core is to make .NET framework open source, and cross-platform compatible so that it can be used in resource-constrained environments. It includes minimum features that are required to run a basic .NET Core app and other advanced features that can be included as a package from NuGet.
Why ASP.NET Core?
- Supports Multiple Platforms: ASP.NET Core applications can run on Windows, Linux, and Mac. So you don't need to build different apps for different platforms using different frameworks.
- Fast: ASP.NET Core no longer depends on System.Web.dll for browser-server communication. ASP.NET Core allows us to include packages which we need for our application. This reduces the request pipeline and improves the performance and scalability.
- IoC Container: It includes built-in IoC container for automatic dependency injection which makes it maintainable and testable.
- Integration with Modern UI Frameworks: It allows you to use and manage modern UI frameworks such as AngularJS, ReactJS, Umber, Bootstrap etc. using Bower (a package manager for the web).
- Hosting: ASP.NET Core web application 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 you to build a class library which can be used with other .NET frameworks such as .NET Framework 4.x or Mono. Thus a single code based can be shared across frameworks.
- Side-by-Side App Versioning: ASP.NET Core runs on .NET Core which supports simultaneous running of multiple versions of applications.
ASP.NET Core - Development Environment Setup
- .NET Core SDK
- Integrated Development Environment (IDE)
.NET Core Runtime and .NET Core SDK are different things. .NET Core Runtime is only used to run .NET Core application whereas .NET Core SDK includes tools and libraries to develop .NET Core applications.
in ASP.NET Core 2.0. Visual Studio now uses .csproj file to manage projects. We can edit the .csproj settings by right clicking on the project and selecting Edit <project-name>.csproj .The csproj file includes settings related to targeted .NET Frameworks, project folders, NuGet package references etc.
Dependencies
The Dependencies in the ASP.NET Core 2.0 project contain all the installed server-side NuGet packages as well as client-side frameworks such as jQuery, AngularJS, Bootstrap etc. These client-side dependencies are managed using Bower in Visual Studio.
Properties
The Properties node includes launchSettings.json file which includes Visual Studio profiles of debug settings.
ASP.NET Core - wwwroot
By default, the 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.
In the standard ASP.NET application, static files can be served from the root folder of an application or any other folder under it. This has been changed in ASP.NET Core. Now, only those files that are in the web root - wwwroot folder can be served over an http request. All other files are blocked and cannot be served by default.Generally, there should be separate folders for the different types of static files such as JavaScript, CSS, Images, library scripts etc.
You can access static files with base URL and file name.
http://localhost:<port>/css/app.css.
ASP.NET Core - 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.
The steps for creating a host in ASP.NET Core 1.x is slightly different in ASP.NET Core 2.x. Let's understand Program class in ASP.NET Core 1.x application so that it will be easy to understand it in ASP.NET Core 2.x.
Setup Host in ASP.NET Core 1.x
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
Every ASP.NET Core web application requires a host to be executed. In the above
Main()
method, we configure a web hosting environment for the ASP.NET Core 1.x web application.
Every ASP.NET Core web application requires a host to be executed. In the above
Main()
method, we configure a web hosting environment for the ASP.NET Core 1.x web application
.UseKestrel()
UseContentRoot(Directory.GetCurrentDirectory())
The
UseContentRoot()
method specifies the current directory as a root directory which will be src folder in the default ASP.NET Core project. The content root directory determines where the content files are located such as MVC view files, CSS, images etc.
.UseIISIntegration()
The
UseIISIntegration()
method specifies the IIS as the external web server or the reverse proxy server.
.UseStartup<Startup>()
The
UseStartup<startup>()
method specifies the Startup class to be used by the web host. Visual Studio creates Startup.cs by default with every new ASP.NET Core application. This Startup class is like Global.asax of .NET framework where you can configure request pipeline (middleware). We may give any other name to the Startup class instead of Startup. We just need to specify it as a generic parameter in UseStartup<T>()
method. And lastly, the
Build()
method returns an instance of IWebHost using the configuration specified above.
host.Run();
The
Run()
method starts the web application and blocks the calling thread till the host is shutdown.Setup Host in ASP.NET Core 2.x
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
The
WebHost
is a static class which can be used for creating an instance of IWebHost
and IWebHostBuilder
with pre-configured defaults. The CreateDefaultBuilder()
method creates a new instance of WebHostBuilder
with pre-configured defaults. Internally, it configures Kestrel, IISIntegration and other configurations.
It also calls
ConfigureAppConfiguration()
to load configurations from appsettings.json files, environment variables and user secrets. The ConfigureLogging()
method setup logging to console and debug window.ASP.NET Core - Startup Class
Startup class includes two public methods: ConfigureServices and Configure.
The Startup class must include a Configure method and can optionally include ConfigureService method.
ConfigureServices()
ConfigureServices method includes IServiceCollection parameter to register services to the IoC container.
The Dependency Injection pattern is used heavely in ASP.NET Core architecture. It includes built-in IoC container to provide dependent objects using constructors.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. You just need to include it in the parameter of the constructor of a class where you want to use it. The IoC container will inject it automatically.
ASP.NET Core refers dependent class as a Service
Configure()
The Configure method is a place where you can configure application request pipeline for your application using IApplicationBuilder instance that is provided by the built-in IoC container.
ASP.NET Core introduced the middleware components to define a 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.
the Configure method includes three parameters IApplicationBuilder, IHostingEnvironment by default. These services are framework services injected by built-in IoC container.
At run time, the ConfigureServices method is called before the Configure method. This is so that you can register your custom service with the IoC container which you may use in the Configure method
IApplication Builder:
Defines a class that provides the mechanisms to configure an application's request pipeline.
.NET Core Command-Line Interface
The .NET Core command-line interface (CLI) is a new cross-platform tool for creating, restoring packages, building, running and publishing .NET applications.
launchSettings.json file : change the
applicationUrl
property and the launchBrowser
property to false, to prevent web browser to start when the project starts.
Program.cs :Method CreateDefaultBuilder(args)
encapsulates all that stuff inside like UseKestrel()
or the UseIISIntegration()
, making this code more readable and quite smaller in Core 2.0.
Startup.cs:In the Startup
class, there are two methods: the ConfigureServices
method for registering the services and the Configure
method for adding the middleware components to the application’s pipeline.
CORS : CORS (Cross-Origin Resource Sharing) is a mechanism that gives rights to the user to access resources from the server on a different domain.
the
Ref: https://code-maze.com/aspnetcore-webapi-best-practices/
Maintain Session in Asp .net Core:
https://benjii.me/2016/07/using-sessions-and-httpcontext-in-aspnetcore-and-mvc-core/
AllowAnyOrigin()
method which allows requests from any source, you could use the WithOrigins("http://www.something.com")
which will allow requests just from the specified source. Also, instead of AllowAnyMethod()
that allows all HTTP methods, you can use WithMethods("POST", "GET")
that will allow only specified HTTP methods. Furthermore, you can make the same changes for the AllowAnyHeader()
method by using, for example, the WithHeaders("accept", "content-type")
method to allow only specified headers.Ref: https://code-maze.com/aspnetcore-webapi-best-practices/
Maintain Session in Asp .net Core:
https://benjii.me/2016/07/using-sessions-and-httpcontext-in-aspnetcore-and-mvc-core/
Comments
Post a Comment