Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / Javascript
Tip/Trick

Angularjs Routing Recipe with Classic ASP.NET MVC Application

Rate me:
Please Sign up or sign in to vote.
4.25/5 (8 votes)
9 Dec 2015CPOL2 min read 19.7K   13   3
This tip describes the angularJs routing and how to configure and work with classic ASP.NET MVC application.

Introduction

In the last two years, I have been learning Angular js Module Pattern. The power of the AngularJS Framework is really amazing and the good stuff is that you can do it quickly. One of the things in AngularJS that impressed me is the routing Framework. Now, I want to try and share some of the knowledge that I have found what is Routing and how to configure and work with classic ASP.NET MVC application.

Background

In a classic ASP.NET MVC application, stepping between parts of the application usually requires a trip to the web server to refresh the entire contents of the page. However, in a Single Page Application (SPA), only elements within the page are updated giving the user a better, more responsive experience.

In classic ASP.NET MVC, implementation may still make transitions to other sections of the application with a full page refresh, but generally a single page manages the application’s functionality through dynamic views and web service (AJAX) calls. AngularJS supports dynamic views through routes and the ngView directive and your MVC application would provide the partial views that the ngView directive will dynamically load.

Using the Code

In this example, the views, called Home, About, and Contact, are simple partial views rendered by the MVC controller.

C#
public class HomeController : Controller
   {
       public ActionResult Index()
       {
           return View();
       }

       public ActionResult Home()
       {
           return PartialView();
       }

       public ActionResult About()
       {
           return PartialView();
       }

       public ActionResult Contact()
       {
           return PartialView();
       }
   }

App.js is the file which is going to have the AngularJS application module declared in it. Also, the state navigation declared. Now, let us see the configurations one by one.

JavaScript
   'use strict';
var app = angular.module('App',['ngRoute']);
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
    $routeProvider.when('/', {
      templateUrl: '/Home/Home',
      controller: 'homeCtrl',
    });
    $routeProvider.when('/About', {
      templateUrl: '/Home/About',
      controller: 'aboutCtrl',
    });
    $routeProvider.when('/Contact', {
      templateUrl: '/Home/Contact',
      controller: 'contactCtrl'
    });
    $routeProvider.otherwise({
      redirectTo: '/'
    });
  }]);

Routes are used by the ngView "<div ng-view></div>" directive in Layoute.cshtml page which has the responsibility of loading the route’s content. As the routes change, the content gets automatically updated.

In Angular, a Controller is defined by a JavaScript constructor function that is used to augment the Angular Scope.

When a Controller is attached to the DOM via the ng-controller directive, Angular will instantiate a new Controller object, using the specified Controller's constructor function. A new child scope will be created and be made available as an injectable parameter to the Controller's constructor function as $scope.

There are three controllers that I have declared:

  • homeCtrl
  • contactCtrl
  • aboutCtrl

Sample code is shown below:

JavaScript
'use strict';
var homeCtrl = function ($scope) {

    $scope.message = "Welcome to Home Page!";
};

angular.module('App')
        .controller('homeCtrl', homeCtrl);

Use this directive to auto-bootstrap an AngularJS application. The ngApp directive designates the root element of the application and is typically placed near the root element of the page - e.g. on the <body> or <html> tags.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Kallol Group of Companies
Bangladesh Bangladesh
I am a software professional always with anticipation to contribute significantly to the IT community. Has a great interest in working with latest technologies like angularjs,knockoutjs,signal IR, web api, asp.net mvc in order to get better results in business / applications. Currently having 5+ years of extensive experience in developing enterprise applications with Microsoft technologies like C#,asp.net mvc,Entity Framework,SQL Server ,WCF,WEB API, ASP.Net under MVC,repository and MVVM patterns and has interpersonal skills with exposure to all aspects of the software development life cycle.
email: zx_programmer@yahoo.com

Comments and Discussions

 
QuestionHi Pin
Member 1261648623-Jul-16 8:55
Member 1261648623-Jul-16 8:55 
QuestionTQ Pin
VCCmu9-Dec-15 5:17
VCCmu9-Dec-15 5:17 
AnswerRe: TQ Pin
Asib Al Amin Talukder9-Dec-15 19:04
professionalAsib Al Amin Talukder9-Dec-15 19:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.