ASP.NET core is a brand new web framework that is open source, cross-platform, cloud optimized and built on top of .NET. The important thing is that ASP.NET Core gives you a consistent and unified experience for building both Web UI and WEB APIs.
Nowadays, we are working in ASP.NET - we have variety of frameworks for building ASP.NET Web pages, ASP.NET MVC, and ASP.NET Web API. And each of these frameworks is works great in software development but there is a drawback with these frameworks which is a lack of consistency among them.
That is,
- Web Pages and MVC both use Razor syntax as they have view syntax. Both use their own HTML Helpers implementation.
- MVC and WEB API both look very similar because they both share a bunch of concepts like Controllers, Actions, Filters, Model binding and Dependency Injection but actually there is no code sharing between them. This is because there may be some reasons like MVC is host on IIS and WEB API will be able to self hosted.
- Because of those variations, we have to write code twice for every feature for both MVC and WEB API.
For solving the above problems, Microsoft provided ASP.NET Core WEB API in which they are aligning (MVC + Web API + Web Pages) into a single aligned web stack for doing both Web UI and Web API’s.
- It has one set of concepts for remove duplication.
- We can implement both Web UI and Web API’s.
- It’s a part of ASP.NET core and supports .NET core (cross platform implementation is possible).
- It runs on IIS or Self-hosted
- Deep integration is possible with Dependency Injection stack.
Getting started with ASP.NET Core
For building WEB API’s using ASP.NET core, first we need to acquire .NET Core from here . Go to that link and click on .NET Core as shown in the below screen.
Once you click on the above link, you will get installation instructions to a particular platform like below.
I am using windows platform so I downloaded and installed the .exe download for my visual studio. You can choose your own platform based on the OS installed in your machine. If you want step by step instructions for working with .NET Core there is an option on the same page just below the above download links. You can choose your platform to work with it. Once you installed .NET Core then start/restart your Visual Studio in administrator mode. I have VS 2017 Which contains .NET core by default, If you are using below VS2017 then the above instructions will be useful. If you are using VS2015 then you have to use VS2015 update 3 for working with .NET core application and also you need to install VS2015 preview tools along with .NET Core SDK.
Once you complete the installation,
Go to File -> New -> Project and select the web node on left navigation in the New Project dialog and you can see the number of options here
In the above options,
ASP.NET Web Application (.NET Framework) is an existing ASP.NET web stack for building web applications like Web Forms and MVC 5 for targeting .NET Framework.
ASP.NET Web Application (.NET Core) is for building ASP.NET Core Web Application and runs on .NET Core. It’s a template for building projects with cross platform compatibility.
We also have an option ASP.NET Web Application (.NET Core) for building ASP.NET Core Web Application which targets to .NET framework, which means it does not have cross platform compatibility.
Now, let’s select the web template which has cross platform compatibility and name the project with some name like “SampleWebApiCoreProject” and click on OK to create the project. After clicking on OK, we will get the new dialog with various options available for selecting a template for.NET Core and select Web API and do not select any authentication for now. Just Keep it as No Authentication and leave the other default settings and click on OK.
It will take a few seconds to create a project in .NET Core. Once the project creation is completed you can see a bunch of files in solution explorer in visual studio. It is just a web api project in .NET Core. You can see that it's just a console application. i.e ASP.NET Core Web API is just a console applciation. You can see a program.cs in this console application as shown in the below screen.
Now, open Program.cs you can see the following code in that class.
- public static void Main(string[] args)
- {
- var host = new WebHostBuilder().UseKestrel().UseContentRoot(Directory.GetCurrentDirectory()).UseIISIntegration().UseStartup < Startup > ().UseApplicationInsights().Build();
- host.Run();
- }
- WebHostBuilder this builder goes to be wired up ASP.NET Core hosting. Once the hosting is done we should run it using Run() for listening the request from client.
- While configuring the web host, we can specify the web server we may want to use like kestrel web server using UseKestrel(), Kestrel is a cross platform managed server.
- We can also specify the content root which means the directory where we want to use the things like views in MVC etc using
- Here we are working with Visual Studio with a console application but in real it runs behind on IIS server internally. Once the request come in, it will redirect the request to a particular server i.e IIS express here. So the line UseIISIntegration() is just enable the integration with IIS.
- Usestartup is to define the actual logic for our application with a start up class. So it will just define which class is used as a startup class in our application. We can create our own class with different name and we can specify the class as a start up class for our application.
There are 3 things which are happening in startup class.
- Constructor
- public Startup(IHostingEnvironment env)
- {
- var builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).AddJsonFile($ "appsettings.{env.EnvironmentName}.json", optional: true).AddEnvironmentVariables();
- Configuration = builder.Build();
- }
- This constructor is for setting up the configuration to our application.
- The .NET Core has a new light weight configuration model which gives us an easy way in name/value pairs using different configuration sources like JSON files, XML files and environmental variables etc.
- The environmental variables are helpful for development because it switches between development and production with some override configurations.
- Configuration Services
-
- public void ConfigureServices(IServiceCollection services)
- {
-
- services.AddMvc();
- }
- The ConfigurationServices method is to setting up all the available services for our application via dependency injection.
- In the above method, MVC is adding the services to service collection and make them available to use.
- Configure
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
- loggerFactory.AddConsole(Configuration.GetSection("Logging"));
- loggerFactory.AddDebug();
- app.UseMvc();
- }
- This method is actually setting up the http request handling pipeline for our application. And this is the code which actually runs behind every request.
- We usually define a set of components that we called as middleware to an HTTP pipeline. When the request comes in, the HTTP request pipeline will look at the request and pass it along or throw an error if something not matches. This will actually define in our application logic.
- There is a little bit of code in the above method which will actually do some kind of logging and use some debugging and use MVC middleware. MVC is actually build on top of generic routing middleware and it sets up MVC as a route handler for that routing middleware. So only one single middleware sets on the application.
We must have at least one controller to see how it is going to work. In our application, we have a values controller which will display the output of the Controller action in browser/fiddler with some values. If you are not familiar with Controllers, I recommend you go through the sample WEB API/ MVC project before going in depth of .NET Core application.