As most of you already know that ASP.NET 5 is now official named as .NET Core 1.0 and ASP.NET Core 1.0 for details you can read the Scott Hanselman blog. So I decided to take you on tour of ASP.NET core 1.0 and to discuss each feature of it, in more developer manner, which will be more on MVC 6 project structure.
1. NuGet References
First of all one of the most notable change in project is the references, all the references are being load in form NuGet packages and to add more functionality into project you need to reference further NuGet Packages. This is one of the useful feature that Microsoft have brought in .Net Core. portable and extendable to everyone's needs. you can carry projects without reference dlls and it will restore all by itself when project loads, secondly you can reduce reference according to your need and each namespace is package by itself.
Details of the all the dependencies are stored in project.json file as shown in figure.
And you can even have this project file in whatever format you like .xml, .ini, but by default it is in .json format.
2. Client-Side Package Managers
Now you can reference client-side libraries through Bower and manage them as a dependencies, by default if you create MVC project you get bootstrap and jQuery dependencies installed through Bower. and you can browse it by expanding Dependencies and then going in Bower folder.
For those who don't now what Bower is, Bower is a Package Manage mainly use in Node.js for downloading client-side Scripts and Style Sheets. Therefore because of its popularity Microsoft has introduced it in .Net Core. You can manage bower package with bower,json present in a root when you enable show all files.
3. Node Package Manager (NPM)
Just like Bower and NuGet, NPM is another package manager that is build for managing Node.js libraries. In .NET Core you can use NPM libraries to en-rich the functionality of the application. By default MVC 6 project contains with Gulp based libraries, In case if you wondering what does Gulp do in ASP.NET project , you have to wait few seconds.
Wwwroot is a web root in which your static content lies. If you further expand wwwroot you will find css, js, lib and images folder. lib folder contains Bower downloaded libraries. Unlike previous version of MVC in which your static content goes within project root.
5. Configuration Settings
If you have used ASP.NET before, you will be familiar of app.config or web.config file that stores global settings and values for the application. In MVC 6 it is by default present in appsettings.json, this file is in json format but you can have it in any other format (ini, xml) according to your liking similar to project.json. if you open this file you will find application settings.
Best thing about this file is that it uses json and json itself is very readable and compact in syntax compared to xml and ini, there are three objects of settings available in this file by default: ApplicationInsight, Data and Logging. ApplicationInsight and Logging is not so important part of the application but it is part of MVC 6 template, for monitoring telemetry data we have ApplicationInsight and for Logging mechanism we have Logging object, lets for get what does these objects to and focus on the Data Object which contains another object named as DefaultConnection and in it contains property of ConnectionString over here you can store Connection string of your database by default it uses localdb instance.
6. Task Runner (Gulp or Grunt)
Since you already know that .NET Core comes with NPM and through NPM you can use many Node.js Libraries in your project, In default template Microsoft provided you with the implementation of Gulp as task manager but you do have option to use Grunt according to your liking. Both Gulp and Grunt used for automating the task and tracking changes in files and rebuild the task back again, therefore over here MVC 6 uses it for cleaning and minifying css and js.
If you remember previous version of MVC you will be aware that it uses its own mechanism to do similar staff, but plus point using Grunt or Gulp is that it is very efficient and takes no time to build libraries.
7. Dependency Injection
MVC 6 comes with a basic dependency injection container and can be configured through Startup service. but you can still move to other DI such as Ninject, Unity and StructureMap etc. For those how don't know what DI is, DI is a special type of service that you can access it through out your project DI are based on 2 concept generally Inversion of Control (IoC) and Service Locator, makes it easy to use and helps in AOP (aspect oriented programming).
8. Startup.cs
Startup is the most important file of the project through which you can configure the behavior of the application, modify request pipeline and loads settings into application. It has a main method that runs the itself, which runs its constructor and within that constructor configuration settings are being loaded from appsettings.json into environmental variables.
Other then that we have 2 separate methods ConfigurationServices and Configuration. In configuration service we register objects as DI service that can be accessed through out application and in Configuration method we configure request pipelines.
Lets examine the ConfigurationServices method.
Highlighted code is the code of service registration used for configuring framework related service.
while below it are the custom implemented service by default we have email sender and sms sender services but it does not have any implementation, you can implement it by going in the Services and write unfinished code to the file MessageServices.cs.
Now we look into Configuration method this is the most important part of this file in which developer can define his own request pipeline. first 2 lines in this method are used by logger and below it an app is being told to use Application Insight for each request for telemetry purpose, and then a check to track environment in order to perform environmental specific task, if it is running in a development environment it will use browser link, Exception page for both code and database operations.
If you look into lines below you will find that UseIISPlatformHandler code this lines initiates request pipelines for IIS server. below that line there an other middle-ware for telemetry data exception, and below it you will find most useful lines that gives functionality to you app, UseStaticFiles gives you static content of the website which are mainly javascript, css and images etc, while below that line an aspnet.Identity that gives functionality of authentication and authorization purpose, and in the end there is MVC functionality which provides you middleware to work on MVC, without this you will not be able to use any of MVC functionality.
COMMENTS