Single Page Application using Asp.Net and AngularJS

First of all, I always having a trouble with jQuery or Javascript. Sometimes back I started using AngularJS and i become a fan of AngularJS in which way that implements MVC pattern and this increased my interest towards JavaScript. Currently I am working in a Single Page Application(SPA) using AngularJS which communicates with the back end through ASP.NET Web API. I really am seeing the beauty of AngularJS these days.
So if you want to learn AngularJS, of course you need to know how to setup a AngularJS project in Visual Studio. There are couple of Visual Studio templates available like AngularJS SPA Template, which you can download and use with Visual Studio. But in this demo, let’s see how you can create AngularJS app in Asp.net.
Lets create empty asp.net Application.
File >> New >> Project
First Screen

Asp.Net Web Application

Select Empty Project

after creating the project install the AngularJS Libraries form the Nuget.

Install AnuglarJS through Nuget

Install AnuglarJS through Nuget

 

 

Alternatively, you can use Package Manage Console to install AngularJS. Once it gets installed, you can see a new folder named “Scripts” added to the project which contains all the AngularJS files needed to start the project off with.

 

 

Now inside my project, let’s create a folder named “App”. The “App” folder is going to be the one folder which will contain all the UI components Like Controller, views etc. Inside “App” folder, let’s create two folders named “controllers” and “views”. “controllers” folder will contain all the AngularJS controllers and “views” folder will contain all the templates (html pages). All the templates will be rendered inside one html page. Let’s create that main html page named “default.html” on root folder.
Create a new Html Page

Create a new Html Page

Add a javascript file on the root named “app.js” and define AngularJS modules there.

 File Structure
app.js
var app = angular.module('myApp', ['ngRoute'])
.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
      .when('/', {
          templateUrl: 'app/views/home.html',
          controller: 'homeController'
      })
      .when('/about', {
          templateUrl: 'app/views/about.html',
          controller: 'aboutController'
      })
      .otherwise({
          redirectTo: '/'
      });
}])
.controller('mainController', function ($scope) {
    $scope.message = "Main Content";
});;

Here in the “app.js”, there are some couple of key components on which AngularJS works. Since our application is Single Page Application, so we don’t want any whole page refreshes or flicking. Here we are using the build in Dependency Injection feature in AngularJS so we are using AngularJS routing capabilities by injecting $routeProvider in AngularJS. There I am saying when the Url is “/” load the “views/home.html” which is bound to “homeController”. When the Url is “/about”, same way load the “views/about.html” which is bound to “aboutController”. There is also a controller defined “mainController”.

 Now lets go and create home and About View and Controllers in app folder. For this first you will create 2 folders in app folder
  1. Views
  2. Controllers

to maintain the separation concern.

Now create a two Javascript files name “HomeController” and “AboutController”. You can use any name convention for the file but i preferred Suffix “Controller” as in MVC to understood the file Role in the application.

the Structure look like this.

Controllers

Controllers

 

Copy this code in “HomeControllers” and “AboutController” to enable them to pass the message to View.

Home Controller:

'use strict';
app.controller('homeController', function ($scope) {
    $scope.message = "This is Home Page loaded using Angular!";
});


 About Controller:
'use strict';
app.controller('aboutController', function ($scope) {
    $scope.message = "This is about page loaded using Angular!";
});
Lets create a view for our application as shown below:
Views

Views

copy the following Html on the relevant view

Home.html

<div ng-controller="homeController">
    <h2>{{message}}</h2>
</div>

About.html

<div ng-controller="aboutController">
    <h2>{{message}}</h2>
</div>
 Let Modify the default.html page to bind all the view and controller together .
default.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="myApp">
<head>
    <title>Sample AngularJS project using Visual Studio</title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/angular-route.js"></script>
    <script src="app.js"></script>
    <script src="app/controllers/homeController.js"></script>
    <script src="app/controllers/aboutController.js"></script>
</head>
<body>
    <div>
        <a href="#/">Home</a>
        <a href="#/about">About</a>
    </div>
 
    <div ng-controller="mainController">
        <h1>{{message}}</h1>
        <div ng-view>
        </div>
    </div>
</body>
</html>
 Here first in <html>, I have mentioned ng-app directive, which powers up the AngularJS. Then in the <head></head> section, I have included all the necessary scripts. Then in <body></body> section, first I have added a <div> which will contain links for navigation. For linking to pages, # (hash tag) is used. We don’t want the browser to think we are actually travelling to “home.html” or “about.html”.
Then defined a <div> which is bound to “mainController”. There first I am displaying the value in $scope.message in “mainController”. Then I have a ng-view which defines a angular template and the templates will be injected inside ng-view.
to See the output right click on the default.html and click view in browser.
Open Page

Open Page

So when we run the project we got the following output

Note the url is “/” here

Home Page

Home Page

 

Note the url is “/about” here

About Page

About Page

 

and when you will find when you click on the other link the whole page will not refresh it loads only the view required. This is the power of AngularJS.

 

I m attaching the sample Project here you can Download this project from Here

 

Enjoy

Regards

Charanjit Singh