ASP.NET Core 2.0 Project Structure And A Few Important Folders/ Files

Despite having been widely accepted as a development platform, ASP.NET was facing a big challenge, in that it was only made for Windows Server and could only be executed on IIS.

IIS is a very popular Web Server (I believe it is the third most popular). IIS supports services like FTP, HTTP, SMTP and enables a Windows machine to manage websites. Because IIS is provided for Windows systems only, therefore, it increases running costs.

Apache is #1 in the race of web server usage. Approx. 48% of websites are hosted on Apache. [Usage of web servers  - https://w3techs.com/technologies/overview/web_server/all]

Apache is an open-source web server, freely distributed and a user can edit the code if needed. The main advantage of Apache is lower cost, enhanced security, and flexibility.

To stay competitive, cost-effective, more secure, and to get wide acceptance in the software industry, Microsoft made significant changes in its development platform for ASP.NET. Since the changes are so wide and broad, Microsoft started this with a completely new name .NET Core (Dot Net Core).

In May 2016, Microsoft launched .NET Core 1.0 and currently, 2.0 is available which was launched in Aug 2017.

.NET Core is open source and supports a wide range of platforms, which means it is not limited to Windows and IIS. Development can also be done in Linux and can be executed on Apache etc.

We will discuss what changes have been made in ASP.NET Core from the project structure point of view and we will slightly discuss the execution pattern of ASP.NET Core application in this article.

Project Structure

  1. wwwroot folder has been added

.NET Core adds wwwroot folder in the project. The term wwwroot is not new but it is still being referred to in Java and IIS. Having a wwwroot folder keeps a clean separation between code files and static files.

Any files which get sent to the browser need to be included in this folder. Like -  HTML, CSS, image, and JavaScript etc. Code files should be placed outside of wwwroot. That includes all of C# files and Razor files.

The wwwroot folder represents the actual root of the web app when running on a web server. Static files like config.json, which are not in wwwroot will never be accessible and there is no need to create special rules to block access to sensitive files.

ASP.NET Core

Please note, in order to server static files to a browser, the UseStaticFiles middleware should be enabled in startup.cs* file.

*We will talk about middleware and startup files later.

  1. Lib folder inside wwwroot

As the name suggests, it is a library folder which holds common library files like jQuery, bootstrap.

Please note, this folder gets updated from bower.json file. So whatever references have been mentioned in bower, those files get updated/created here.

There is no need to check in these files/folders in your code repository as bower maintains these files whenever you restore the package.

ASP.Net Core

  1. json

Bower is the package manager for the web while NuGet is used to deliver .NET assemblies.

There was always a need and there was no answer so far for what we should do for client-side dependencies. In ASP.NET Core, Bower was able to provide that answer.

Within the .NET ecosystem, it fills the void left by NuGet's inability to deliver static content files. For ASP.NET Core projects, these static files are inherent to client-side libraries like jQuery and Bootstrap. For .NET libraries, you still use NuGet package manager.

ASP.Net Core
Sample Bower.json File

Bower.json file keeps the list of all client side dependencies in a common place, which makes it easier to manage and easier to update (if needed).

ASP.Net Core

  1. JSON

In the previous versions of ASP.NET, we needed to write the code to control bundling and minification. Bundling and Minification are used to improve the performance of web applications by reducing the size of the requested assets, such as JavaScript and CSS files

.NET Core provides built-in features/built-in tools or commands for bundling and minification.

Bundling is a technique in which we combine multiple JavaScript or CSS files into a single file so that the server needs to serve fewer files. Minification is a technique to reduce the size of the file by removing white space and commented code. That means the server needs to serve smaller files.

Both bundling and minification play an important role in web application performance.

ASP.Net Core
Sample bundleconfig.json file

  1. CS file

ASP.NET Core is a console application which means that the runtime environment looks for Main() method, like what we were doing earlier in the actual console app. Earlier, the runtime environment (or web server) used to look for global.asax file to start the application or server. Now, Runtime environment looks for Main() method in Program.cs file.

As you can see below, main() method calls the CreateDefaultBuilder() method which actually creates web application host. The CreateDefaultBuilder() encapsulates some default functionality, e.g. - UseKestrel(), UseStartup() etc.  Full definition of this method can be read on GitHub –

https://github.com/aspnet/MetaPackages/blob/dev/src/Microsoft.AspNetCore/WebHost.cs

There is quite a bit of difference between core 1.X and 2.0 about Program.cs. I found the below article very useful to describe the difference –

https://andrewlock.net/exploring-program-and-startup-in-asp-net-core-2-preview1-2/

ASP.Net Core

  1. CS file (Startup Class)

UseStartup method in your Program.cs file uses the name of the startup class. This class is always a public class and contains two important methods.

  • ConfigureServices(Optional)
    This is used to configure the services being used in the application.

  • Must include a Configure method
    This is used to create the application request processing pipeline.

    1. ConfigureServices method

      • This is the optional method.
      • It gets called before configure method.
      • Called by the web host before configure method.

        ASP.Net Core

        Adding services to the service container makes them available within the app and in the Configure method.

        Web host may configure some services on its own while some other features require substantial setup. To do that, there are Add[Service] extension methods on IServiceCollection interface which are used.

        Generally, a web app registers services for Entity Framework, Identity, and MVC.

    2. Configure method

      Configure method is a must have method in Startup class. This method takes 2 parameters 1. Object of IApplicationBuilder and 2. Object of IHostingEnvironment.

Configure method defines how the application will respond to HTTP request and in order to do that, it adds middlewares to the Applicationbuilder as below.

ASP.Net Core