Everything you need to know about ASP.NET 5 Project Template

Its been a quite while that ASP.NET 5 is out there. Idea of this post is to help migrate to the new project template that comes with Visual Studio 2015. Lets get started.

OK, first things first.. lets get started with a new project. In the Visual Studio 2015 just >

New Project Template Dialog

In this dialog box make sure you have select ASP.NET Web Application and fill in the details.

Name : MyFirstASPNET5
Location: Where your project files gonna store
Solution name: MyFirstASPNET5

And click OK.

In the New ASP.NET Project dialog box select Web Application under ASP.NET 5 Preview Templates option. (NOTE: ASP.NET 5 is still on its preview stage.).

New Project Dialog

For the moment we are not going to create any test projects for this tutorial. After selecting Web Application under ASP.NET 5 Preview Templates click OK button. Visual Studio might ask you to create a Azure Website using this project template. If you do not wish to create a Azure Website just click Cancel in the next dialog box. Now wait for few seconds till Visual Studio create the base template for you. This base template is a starting point for your ASP.NET 5 Web Application. And its a fully working start point for you. By just pressing you can go through this base template.

This is how it looks like
Base Template

Press for a quick preview of this template up and running. (ASP.NET5 running on Microsoft Edge)
ASP.NET 5 on Edge

OK, so far everything is working fine and lets close the browser and go through the Solution Explorer. By default you should see the Solution Explorer is in the right hand side of your Visual Studio 2015 window. If not click View > Solution Explorer or just press CTRL + ALT + L 

View Solution Explorer

The Solution Explorer

The Solution ExplorerASP.NET project template will look like this. It might not change a much when the full release is available. As you remember a total new way of keeping the configurations and the folder structures.

You have a Solution Items folder that holds the global configurations for the solution. Yep you are correct its a JSON file. Also you get these References for your project as usual. And a whole new wwwroot section to hold the static files. Again as usual Controllers, Models and Views. And a Dependencies for your project dependencies.

And code first Migrations are enabled by default and it got is on folder in the root level. And all other JSON configuration files.

Finally a whole new folder for the Services you use, such as sending emails from you web application.

global.json

This file contains mainly two parts. The detail of the project and the sdk used by the whole solution.

Source FolderThe projects property has values to tell where your source code is stored. By default it stores under a folder named src . All your source is stored under this folder. Yes you can change this name without any problems. This will help all the build artifacts to store in the different folder named artifcats  and can be easily omit from the source controlling. Can keep your code much more organised by default. Cool isn’t it.

And sdk will store the version of the DNX (.Net Execution Environment) which Visual Studio will use it when you load the solution. Yes you can store different SDK values in the project.json file for different projects that use different versions of SDKs in the same solution.

wwwroot section

wwwroot SectionThis sections stores all your content files. As you see it has css, images, all your javascript files and the different libraries you use to make your development much more easier.

In the previous versions of ASP.NET the web root was the project root of the solution. It allowed to access all the static files as well as the configuration files such as web.config and global.asax. For protecting these configuration files you had to write specifically to protect these files. Also known as blacklisting.

But creating a separate section for all these static files in this new template and make it your root of the site. You do not need any extra effort for protecting. wwwroot just contains all the static files and other content that needs to start off your website.

Apart from the security this separation will simplify the minification and bundling of your static files such as css and javascript files.

Dependencies

Client side dependencies The Dependencies section will hold all he client side stuff that your project will depend on. It has two folders, Bower and NPM. These folders hold two package managers (like NuGet) with corresponding names. These two will pull all the dependencies as per seen in the image.

The bower dependencies are controlled by the bower.json file.

As you see each dependency can be controlled separately within this file. Also indicating how it should be deployed to the wwwroot folder when the bower task is executed.

NOTE: All the server side dependencies are controlled under References section. 

config.json

ASP.NET 5 no longer stores its application wide configuration settings using XML. Previously this file is known as web.config. All these settings are stored under config.json using JSON format. By default the file should look like this (at final release the content might change).

Again there is nothing fancy or you must have this file for your configurations. Not like ever growing web.config file you can have separate configuration files in a project. This main file by default accessed using Startup.cs in your root folder.

As you can see in the Startup method the builder variable collects all the configuration files you have in your project. So you can keep all your configurations in separate files for easy access and for maintainability and for the readability.

Summary

ASP.NET 5 introduces a few concepts that didn’t exist in previous versions of ASP.NET. Rather than working with web.config, packages.config, and a variety of project properties stored in the .csproj/.vbproj file. And developers can work these separate folders and files for maintainability and for security. Yes there will be a learning curve and its worth it !!!

The Default Route

When you press to debug an ASP.net MVC application you will see the URL is simplified and easy to understand and can use as a navigation as well. For an example when you click on the “About” link of the default ASP.net MVC application you the URL visible will look like this.

url

As you see after the domain name (in this case the localhost with the port number) it says Home and the forward slash and About. So it looks like you might have a Home folder and then About folder and there is no file name given end of the URL (ex: About.aspx). But you have a page displayed. If you wonder how this is possible, the trick is under routing.

OK, where this is handled?

Click on the Solution Explorer and expand your web project (in this example WebApplication5) and expand App_Start folder. Double click on the file, named RouteConfig.cs

RouteConfig

Now scroll down to the code that looks like this

From line 7 to 11 you will see the route collection is being added. This is called the “Default Route”. It has three parameters

name: Name of the the route, in this case its “Default”

url: Which is the pattern of the URL that this route will create. {controller} represent the actual Controller of the MVC application. In this example its the HomeController and {action} represents the action within the controller again in this one its the About action within the HomeController and {id} is the parameter that takes in to an action method with in the controller.

defaults: These are the defaults values to replace above url pattern. And its self explainary. And says id parameter is optional.

Good Day !!! 🙂

Choosing an ASP.net Flavour

At the beginning there was only ASP.net Web Forms. Now you have three different models to choose with.

  1. ASP.net Web Forms
  2. ASP.net MVC
  3. ASP.net Web Pages

ASP.net Web Forms

These are set of pages that has a UI (User Interface), Server Controls and Server Code. When a user sent a request to the page using the browser, it goes to the server,  then compiled and executed at the server side. Finally generates HTML markup that can be rendered on your browser. If you are a experienced Windows Forms developer its a good choice to choose Web Forms. It is the event driven world that you already familiar with.

Server Controls such as Button, Grids, Labels, DropDowns can be easily dragged and dropped to your design and start coding like you did with Windows Forms.   To write server code to handle the logic for the page, you can use a .NET language like Visual Basic or C#.

More details on ASP.net Web Forms

ASP.net MVC

The MVC stands for Model View Control. This is a design pattern most developers are familiar with. This is a light weight framework that can be used with ASP.net features such as Membership based authentication and Master Pages. This pattern separates your application into three parts. The logic of the application goes under Model. The presentation layer goes under View and finally talking between model and the presentation layer goes under Controller. Again to write code and handle the logic you can use a .NET language like Visual Basic or C#.

More details on ASP.net MVC 

ASP.net Web Pages

This flavour gives you a taste of script based programming like you did with classic ASP or PHP. A special syntax called Razor will escape your HTML code to dynamic code using a .NET language like Visual Basic or C#. If you know HTML, CSS and little bit of JavaScript and you have programmed with a .NET language like Visual Basic or C# and you are ready to start building ASP.net Web Pages.

More details on ASP.net Web Pages 

Have a Good Day !!! 🙂

Extending ASP.net Identity in an ASP.net MVC 5 application

A typical ASP.NET MVC application comes with a system for User Registration, Login and other elements you need to control your website that uses an user management system. This is more than enough for a typical website. In some cases you might need to extend this functionality. For example add few fields to your Registration Form. Before starting this tutorial make sure you have downloaded and installed Microsoft Visual Studio Express 2013 for Web (Link: http://bit.ly/1gNqqvJ). If you already have it or you have another Visual Studio 2013 version no problem lets gets started and skip the downloads.

Why Express Editions ?

Express editions are FREE to download from the Microsoft. Can be easily grab and install using above link. Express edition has everything you need to complete this tutorial. And this tutorial uses Microsoft Visual Studio Express 2013 for Web.

OK, Lets get started

First of all let us build a new project using Microsoft Visual Studio Express 2013 for Web. Lets click >

New Project

In the window, expand Templates and then Visual C# and select Web. In the Templates pane select ASP.net Web Application.  In the Project Name text box type a project name. In my demo I gave IdentityDemo. Now click OK

New ASP.net Project

Now you will be presented with New ASP.NET Project dialog box. Now select MVC in the Select template: area and make sure MVC check box is checked, under Add folders and core references for: section and finally click OK.

Now you have everything to test this demo. To confirm everything is working fine. Press , and the default project will be compiled and launched in the default web browser. If everything is OK you should see this screen.

IE

Lets click on the Register link to see what we have there. A default registration page will be displayed. This registration form has only a user name and a password (yes confirm password too). So its a basic registration form. What if we need to capture more data. For an example I need to capture an email. How do I add this to the default registration system without breaking it.

Registration Page

Lets get an user registered here and check what is the default system has to offer. Like a normal registration form type a user name and a password and click the Register button. After a few seconds you get registered and logged in to the system.

Hmm, what happened in the background ?

At this moment you must be wondering whats going on in the background. Where is the database, where is these data stored. How do you check these data. By default ASP.NET MVC 5 project template has connection to a local database file under App_Data folder in the Solution Explorer. By default you wont see anything under there. First you need to click the icon Show All Files. This will show all the files that not included in to this project.

Show All Files

Now expand the App_Data folder. You will see a MDF file with the project name and the date. Double click on this file and you will able to see the structure of the database and its tables. You will notice (Server Explorer will be opened with your database). If you have used or familiar with ASP.NET Membership and seen tables created. But ASP.NET Identity has created a different set of tables and there are no more long names, stored procedure and views. Spend some time exploring these tables.

Database - SE

As you see under Server Explorer under Data Connection and you expand the Default Connection. Default Connection is the connection string name to your database. The table names are straight forward and understandable. Now you have seen the database. Lets explorer the code.

By default all these elements are controlled by the AccountController.cs (in the Solution Explorer, expand the Controllers folder). And the models can be found under Models folder. And the file name is AccountViewModels.cs. Our goal is to add a new field to the registration form. For example we add a new Email field to the registration form. You may think simply add an Email field in the AspNetUser table and update the view. Thats what I thought in the first place. It doesn’t work that way. The Identity model is developed using Code First method. So we need to follow that method to add a new field.

OK, Where is these information stored ?

If you expand the Models folder. You will see and extra file than the AccounttViewModel.cs which named IdentityModel.cs which as two simple classes implemented.

OK lets look at the class. This is the class we gonna add properties to the user. In this case an Email address. This class inherits the IdentityUser which has the model for the AspNetUser table. This class implements the IUser interface. Again the class will be link the actual database to the ApplicaitonUser. This is the place where the connection has been made to the database and to the table AspNetUser. Lets add a property named Email to the class. The changed code will be.

So far so good. Now Lets modify the code at AccountViewModel.cs and add the new Email field. The class we going to modify is . Why we doing this? Because, type is used to generate the Register.cshtml view, which is a strongly typed view of type. All the validation code can be implemented here. Lets see the modified code of the .

The new field added in to the class (see line 18, 19 and 20) also validated and given an error message.

Now lets go and modify the View for the registration page to reflect this change. Expand the Views folder under solution explorer. Again expand Account folder. Now double click on the file named Register.cshtml. This file has all the necessary code for the default registration system. Now we are going to add the new Email field we created. This is how this file looks by default.

Lets add the below code to the Register.cshtml below confirm password field (this is after line 31 in above code, or any place with in the form).

OK, we are ready to test our code. Simply press to test our code. All seems to be good. Site loaded without and compile error 🙂 and now click on the Register link. YaY !!! you can see the new email field is added. With all the validation stuff working.

Email field added

Lets just type some valid information and click the register button.

8

OK all the happiness just we had is gone. A runtime error has been thrown. What does this error message means. In simple words its says “OK, dude you have added a field in the class remember? Its not in the database duh!!”

Ah, OK Shall I go and modify the database and check?

No, you cannot. Remember at the top of this post I have mentioned ASP.net Identity is implemented under CodeFirst approach. We need some help from out side to update the database using newly made changes to the class. Where can we get this help? Yes you already have the correct tool in the Visual Studio it self. Lets open the Package Manage Console by using > > . This will open the . In the console prompt type with - parameter and press . After a successful command console window will look this.

10

Now you have enabled the CodeFirst migrations. Lets update the database issuing command. And after a successful command the console window should look like this.

11

Have a look on the Server Explorer and expand the database. You will see the newly added field is there.

12

OK, we are almost done. Lets not get excited yet. We need one more change to be done to store the newly added email data to the database. Lets open the AccountContoller.cs  in the Solution Explorer under Controllers folder. Move to the override method (action) of Register (which has the HttpPost attribute).

Change the code in line number 8 to reflect this

Phew !!! we are done now. Press and test your code.

Congratulations Coder !!! You have just extended the ASP.NET Identity  🙂

 

Where did they hide the web templates in Visual Studio 2013

For the first time when I loaded the Visual Studio 2013, just clicked on the “Web” label in the right side pane under Templates in dialog window, just felt OK where are my web templates.

New Project

OK, to get those now you need to go through one extra step. Just select on the Label “ASP.net Web Application”  and choose a folder and give a proper name to your project and click “OK”. The next dialog box has plenty of information you need know about your web application.

Select Template

1. This area will help you toe select a starter template. It has

Empty – A blank skeleton to start from scratch, You will need to add References, Folders for your project by hand.

Web Forms – A Web Forms project template will be created here. Most of the References you need to work with a Web Project will be loaded for you and all the necessary folders and files created. You will have a basic web site working here, to test just simply press “”.

MVC – This option will create a template with Model, View, Controller pattern. All the required folders, scripts and references will be automatically created for you. A sample web site is already ready for you to test. Simple “” will get you the sample web site up and running.

Web API – This template will allow you to create a RESTful HTTP Service. A broad range of clients will be able to access this service when you are done.

Single Page Application – A template that allows you to create a website with rich client JavaScript, HTML5 and CSS3. With a mix of Web API.

Facebook – A ready to run facebook application will be created with this template. A great template to start your facebook application.

2. An informative area of the selected template on area 1. A small description is displayed here for the selected template.

3. This area will allow you to add references or remove them on a selected template on area 1. If you select add unit testing, you will able to give the name for the testing project name here as well.

4. This button will allows you to select the authentication type of the selected project template type.

Enjoy 🙂