Nowadays it is one of the hot topic for Asp.net interview. If you are attaining any interview then they will ask the questions from this topic. So i m going to write the series of post from basic to advance concept on this topic.
Question 1: what is the asp.net core ?
>> It is the next version of asp.net. It is a free,open-source,high-performance and cloud optimized web framework which can run on Windows, Linux, or Mac. This framework is completely rewritten from scratch for building modern, cloud-based, Internet-connected applications
Question 2: Why should we use asp.net core ?
>> These are the following reasons to use Asp.net core
1. ASP.NET Core is faster compare to traditional ASP.NET.
2. Now It is single application for web api and Web UI Layer.
3. Architected for testability.
4. Razor Pages makes coding page-focused scenarios easier and more productive.
5. Blazor lets you use C# in the browser alongside JavaScript. Share server-side and client-side app
logic all written with .NET.
6. Ability to develop and run on Windows, macOS, and Linux.
7. Open-source and community-focused.
8. Integration of modern, client-side frameworks and development workflows.
9. A cloud-ready, environment-based configuration system.
10. Built-in dependency injection.
11. A lightweight, high-performance, and modular HTTP request pipeline.
12. Ability to host on IIS, Nginx, Apache, Docker, or self-host in your own process.
13. Side-by-side app versioning when targeting .NET Core.
14. Tooling that simplifies modern web development.
15. Built in logging support.
16. Environment based configuration supported for cloud deployment.
17. In ASP.NET Core we have middleware which provides more control how the request should be processed
as they are executed in the order in which they are added.
Creating Hello World application on Asp.net Core
Step 1: Firstly install the visual studio 2019 or 2017.
Step 2: Open the Visual Studio 2019 and create the Empty Asp.net core application like this
Step 2: Select the type of project, we are going to create
Now it will create the following files
All files in the root of the project are
Program.cs
>> It is the entry point for web application. Everything starts from here. The .NET Core host can only run console applications. So, the web app is a console application too.
using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace HelloWorld_Demo { public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); } }
This class role is to create the host for the web application. It is the right place to configure the hosting related setting on different server. As you can see in above class which has CreateDefaultBuilder method. Which configure all the services needed for us.
Startup.cs
Asp.net core pipeline start from this file.
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace HelloWorld_Demo { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) => { await context.Response.WriteAsync("Hello World1!"); }); } } }
This class has ConfigureServices Method, where dependency injection will be configured and Configure, where all needed middleware components are registered and configured.
The app.Run method is a perfect example of an inline middleware component.
ASP.NET Core offers an interface named IHostingEnvironment which is used to reads a specific environment variable called ASPNETCORE_ENVIRONMENT and checks its value. If it is Development, it means you are running the application in development mode. If it is Staging, you are running the application in a staging mode. And so it goes for all the environments you need to manage.
We can change the Environment using this file launchSettings.json which will be there in properties folder of application.
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:64471", "sslPort": 44320 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "Key": "Value", "ASPNETCORE_ENVIRONMENT": "Development" } }, "HelloWorld_Demo": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "https://localhost:5001;http://localhost:5000" } } }
appsettings.json
It is similar to webconfig file of asp.net application. Which is used for storing data as application level. We can store the connection string like this
{ "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { "MvcMovieContext": "Server=(localdb)\\mssqllocaldb;Database=MvcMovieContext-2;Trusted_Connection=True;MultipleActiveResultSets=true" } }
Now run the application, we will see the hello world in browser like this