Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET 5 - Part II

0.00/5 (No votes)
10 May 2015 1  
In this section, we'll delve further in the new ASP.NET 5 project.

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 carried forward like Model, View, Controller, clean separation of concerns and many 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 is to maintain the clean separation between files at the web-server retrieved and sent to the client and the files which contain the logic and configuration. Static files like JS, CSS, HTMLs can live in WWWroot folder.

15th

Now, let's suppose when I run the app and try to see the image which I have placed in My Images folder, then it will give me a 404 error.

16th

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 the result.

18th

17th

So, the point is all static files are served by root folder and logical stuff 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 is 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. First, let’s see project.json file. This file now manages many aspects of website.

19th

The first thing which you will notice here is that root folder is set to WWWroot here. So, this is the place which is telling the 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 the 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 ways. One way which we used is already shown below in the screenshot.

20th

21th

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:

22nd

Also, I still have the flexibility of different nuget source.

23rd

The second option is via project.json file. Let’s suppose that I am planning to install some custom package. Then I can do as shown below as well:

24th

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. The first one is the core version and the second one is the cloud optimized version.

25th

Now, let’s collapse the same and drill into it.

26th

Now, here the dependency management system will keep your assemblies in a nice tree structure. So, it nicely tells which package is dependent on which package.

27th

Now, as far as Framework and runtime are concerned, as you can see below in the screen shot, I have both frameworks listed here.

28th

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.

29th

So, as you can see 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 the desired result.

31th

But, when I explicitly build the project and checked the output window, then it gave me the below error message.

32nd

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, you can just comment the Core CLR section in the project.json file as shown below in the screen shot.

33th

Now, as soon as I commented the above section, the below references got refreshed automatically.

34th

Now, when I build the app, then it will build fine.

35th

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 to the conditional build as shown below.

36th

With this, I would like to wrap this second session of ASP.NET 5 New Features. We will delve further in the coming series. Till then, stay tuned and happy coding!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here