ASP.NET Core default project content
Post about creating ASP.NET Core project very briefly described how to setup project without much thinking, this time it’ll be a little more in depth. I’ll describe what initial project contains, what are differences between ASP.Core and ASP.NET MVC, what is new. Setup is same as before – empty ASP.NET Core project for .NET Core 1.1 with Web Application template, Individual Accounts authentication and without Docker support.
So this is how empty looks like, If you had previous experience with ASP.NET MVC you’ll quickly identify new stuff:
Let’s start from the top with Connected Services. This option is for handling connections mainly for another Microsoft services hosted in Azure. Default option here is Application Insights, which is used to monitor how users use your application and how it behaves.
Next, we have Dependencies, which replaced References:
As you can see it’s now split into three categories – Bower, NuGet and SDK. Bower is an external package repository created for web stuff – CSS and javascript libraries. I really like this solution, in my opinion, it is much clearer than previous, when we were handling such packages through NuGet. NuGet is still here though, but this time only for .NET related stuff. The last category is SDK which contains most basic references for .NET Core application. There is also a fourth category – Assemblies – for references added manually to DLLs on your disk and fifth – Projects – for references to other projects in the solution. They appear only if you have any. List of dependencies above contains represents what is initially added to your project. I believe it’s good to be aware what is actually added to your project.
After Dependencies we can find Properties:
In previous versions here is where AssemblyInfo.cs file resided. This file is no longer created for .NET Core projects – it’s content was moved to project settings. Instead, in Properties category we have launchSettings.json file, which describes environments.
Next on list, we can find wwwroot folder, which contains all static files for website:
The content is, I believe, self-explanatory for anyone who developed any www page before, I’ll only mention here lib folder where downloaded bower dependencies reside.
After wwwroot, we can find directories with actual source code – Controllers, Data, Models, Services, Views. As before – not much changed here, I’ll mention Data directory when we can find Entity Framework stuff and new file _ViewImports.cshtml which contains using directives, ASP.NET Tag Helpers initialization and more.
Next, in project, we can find applicationsettings.json file which I described in post about a configuration, bower.json file which represents bower dependencies I described above and finally bundleconfig.json file, where now bundling and minification is now setup.
At the end, we have two cs configuration files – Program.cs and Startup.cs. Both of them have same obligations as Global.asax file in the past – they mainly handle application startup process. Name Program.cs is definitely familiar to you – it’s the name of the file that usually contains entry point for the application and it’s same here:
I won’t be describing bit by bit what’s going on here because I believe names are self-explanatory. I’ll mention Kestrel though – it’s new web server designed for performance and interoperability, you can read more here.
Startup.cs file handles configuration described previously, dependency injection and HTTP pipeline. ASP.NET Core has custom DI container, which is great for basic purposes. Usage is simple:
We have standard framework’s methods – AddDbContext, AddIdentity and AddMvc to register stuff related to infrastructure. Later in code, we have AddTransient and AddSingleton methods to manually register our custom classes. Nice addition with the DI being a integral part of ASP.NET is @inject statement allowing us to inject services directly on view.
HTTP pipeline describes how each request should be handled, it’s configured in this method:
We have here setup logging, exceptions and routing amongst other stuff. This is a place when you can add middleware – the new mechanism allowing you to add custom logic to handle each request, simplest example – logging request data.
I hope you liked this brief explanation of new stuff and differences between ASP.NET Core and ASP.NET MVC. For now thanks for reading and see you next time! 🙂