In this section, we’ll delve further in the new ASP.NET 5 project. When you take a closer look at the ASP.NET 5 project, you will see that there are many things which are carry forwarded like Model, View, Controller, clean separation of concerns and may more. But, there are also some significant changes around ASP.NET 5. Now, the root of the website is no longer the root of the project. By default, root will be WWWroot folder. The idea behind this to maintain the clean separation between the files at the web-server retrieved and sent to the client and the files which contains the logic and configuration. Static files like JS, CSS, HTMLs can live in WWWroot folder.
Now, lets suppose when i run the app and tried to see the image which i have placed in My Imgaes folder, then it will give me 404 error.
On the other hand, if i go ahead and add the same folder in WWWroot folder and try to navigate the same then it will produce me the result.
so, the point is all static files are served by root folder and logical stuffs served by project space as we initially saw, when i added controller in the controller’s folder and it took effect. One more thing you might have observed that there is no web.config file now in the solution. Also now there is no Global.asax file; however the same is replaced by startup.cs file. But, we’ll see this later. 1st let’s see project.json file. This file now manages many aspects of website.
First thing which you will notice here that root folder is set to WWWroot here. So, this is the place which is telling website that this is the root folder. So begin the show from here. This you can change as well or rename if you like to. Now, this configuration file is in JSON format. This is also telling the ASP.NET runtime what dependencies project is going to need. In this new ASP.NET 5 system there is a new way to manage dependencies. No need to reference assemblies and store the lists of referenced assemblies in the project file. Instead we refer to nuget packages as dependencies and these dependencies are listed in our project.json file. Now, there are couple of reasons for this change. One reason is to simplify the dependency management. Another good reason for this is that ASP.NET is moving away from VS dependency. So, in future i can use any text editor to build the ASP.NET App.
Now, these dependencies can be both the ways. One way which we used already shown below in the screen shot.
Now, the UI of this also changed. Initially we used to have Modal window. Now, this is more like complete screen giving wider visibility. I can also see the installed templates like
Also, i still have the flexibility of different nuget source.
2nd Option is via project.json file. Let’s suppose i am planning to install some custom package. Then i can do like shown below as well
However, whatever package you install; you can find its references under “References“. Below, in the screen shot you can see that there are two versions of runtime here. 1st one is the core version and 2nd one is the cloud optimized version.
Now, let’s collapse the same and drill into it.
Now, here dependency management system will keep your assemblies in nice tree structure. So, it nicely tells which package are dependent on which package.
Now, as far as Framework and runtime is concerned as you can see below in the screen shot; i have both frameworks listed here.
So, whenever i am building the solution i am building against both of these frameworks. By building against both i am actually ensuring that it will work well against both the frameworks. This also means whenever i am switching platform my code won’t break there.
But, let’s go ahead and break something here. You remember in the last segment i created one new controller with a notepad. Now, let’s modify the same and refresh the app.
so, as you can see that it says that GetCallingAssembly() which is part of System.Reflection is not supported in the cloud optimized version. But, let’s suppose i overlooked this error and refresh the app.So, it actually produced me desired result.
But, when i explicitly build the project and checked the output window, then it gave me below error message.
so, building with error but application is working fine with full blown CLR. Now, if you have decided that you don’t want CORE CLR version. so, you can just comment the Core CLR section in the project.json file as shown below in the screen shot.
Now, as soon as i commented the above section, below references got refreshed automatically.
Now, when i build the app then it will build fine.
But, suppose you want the other way means you would like to keep the cloud optimized version also and build should also succeed. For this scenario i need to refer the conditional build as shown below.
With this i would like to wrap this 2nd session ASP.NET 5 New Features. We will delve further in the coming series. Till then stay tuned ad Happy Coding.