Expand Minimize

Always specify default route

Always specify default route to gracefully handle requests that don't match any route

CheckId NG1010101
TypeName AlwaysSpecifyDefaultRoute
Severity Error
Type AngularJS Module

When using routing in AngularJS you should always specify the default route to gracefully handle requests that don't match any route.

Good practice

angular.module('app').config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/', {
      templateUrl: './spa/app/home/home.html',
      controller: 'home',
            controllerAs: 'vm'
    })
    .when('/:id', {
      templateUrl: './spa/app/detail/detail.html',
      controller: 'detail',
            controllerAs: 'vm'
    })
    .otherwise({
      redirectTo: '/'
    });

}]);


Bad practice
angular.module('app').config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/', {
      templateUrl: './spa/app/home/home.html',
      controller: 'home',
            controllerAs: 'vm'
    })
    .when('/:id', {
      templateUrl: './spa/app/detail/detail.html',
      controller: 'detail',
            controllerAs: 'vm'
    });
}]);

Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.