In this post, we are going to create a Single Page Application (SPA) with AngularJS and ASP.Net Core. We will use Angular-UI-Router for our application routing instead of MVC routing.
If you are new to ASP.Net Core, get a complete startup overview here.
Prerequisites
Before getting started make sure development environment is prepared properly. These are prerequisites to develop the sample application.
- Visual Studio 2017
- .NET Core 2.0
- NodeJS
- MSSQL 2016
- IIS
Key Points
The following points will be discussed to develop the sample application:
- Manage Packages
- Transfer Libraries Using Gulp
- Working with AngularJs App
- Adding Middleware
- Scaffolding MSSQL Database
- Web-API
- Enable CORS
- Build AngularJS App Using Gulp
- Test Application (CRUD)
- Publish in IIS
Getting started
Let’s get started. Open Visual Studio to create a new project, Goto > File > New > Project then choose a project for creating ASP.Net Core application with ASP.Net Core 2.0 MVC Web Application like below screenshot.

Initially, application folder structure might look like this, we are going to modify those following our requirements.

We are using AngularJS routing to create the Single Page Application by ASP.NET static file serving.
We need to point a static page to get all the requests, then Angular will handle the request and decide which page to serve. Let’s create HTML page in a wwwroot folder. This will be our main page to load on client request.
Serve Default Page
Now, we need to modify Startup.cs file to add middleware for serving the static HTML page. Here is the code snippet which is going to serve the page. In this app, I have created it as “index.html”.
- DefaultFilesOptions options = new DefaultFilesOptions();
- options.DefaultFileNames.Clear();
- options.DefaultFileNames.Add("/index.html");
- app.UseDefaultFiles(options);
Middleware to Handle Client Side Routes Fallback
To avoid 404 error while reloading the page in AngularJS SPA app we need to add middleware to handle client side route fallback. The below code snippet will take care of that.
- app.Use(async (context, next) =>
- {
- await next();
- if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
- {
- context.Request.Path = "/index.html";
- context.Response.StatusCode = 200;
- await next();
- }
- });
Get more details on middleware here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?tabs=aspnetcore2x
If we run our application to test, the static page will serve by default. Now let’s get started with client-end package managing.
Client-Side - Package Manage
We need to add some frontend package like AngularJS, as we can see our initial template has nothing like that.

To add new package right click on a project then click on “Manage Bower Packages”


Install the required package by searching one by one. Finally, the installed packages will be listed like below screenshot.
Let’s add some Node Packages to our application, first we need to add npm config file to our project. Right-click the project Goto > Add > New Item.
From the new item add window choose npm Configuration File like the below screenshot.

Add all those packages to devDependencies, it’ll automatically install to our project.
- "gulp": "^3.9.1",
- "gulp-concat": "^2.6.1",
- "gulp-rename": "^1.2.2",
- "gulp-cssmin": "^0.2.0",
- "gulp-uglify": "^3.0.0",
- "gulp-htmlclean": "^2.7.20",
- "rimraf": "^2.6.2"
Finally our package dependencies are listed.

Let’s transfer the required libraries from bower_componen folder to “wwwroot” for calling it to the main HTML page.
Add a gulp file then copy the below code snippet and paste it to the newly added file.
- /// <binding AfterBuild='build-all' />
- var gulp = require("gulp"),
- rimraf = require("rimraf"),
- concat = require("gulp-concat"),
- cssmin = require("gulp-cssmin"),
- uglify = require("gulp-uglify"),
- rename = require("gulp-rename");
- var root_path = {
- webroot: "./wwwroot/"
- };
- //library source
- root_path.nmSrc = "./bower_components/";
- //library destination
- root_path.package_lib = root_path.webroot + "lib/";
- gulp.task('copy-lib-css', function () {
- gulp.src('./bower_components/bootstrap/dist/css/bootstrap.min.css')
- .pipe(gulp.dest(root_path.webroot + '/css/'));
- gulp.src('./bower_components/toastr/toastr.min.css')
- .pipe(gulp.dest(root_path.webroot + '/css/'));
- });
- gulp.task('copy-lib-js', function () {
- gulp.src('./bower_components/jquery/dist/jquery.min.js')
- .pipe(gulp.dest(root_path.package_lib + '/jquery/'));
- gulp.src('./bower_components/bootstrap/dist/js/bootstrap.min.js')
- .pipe(gulp.dest(root_path.package_lib + '/bootstrap/'));
- gulp.src('./bower_components/toastr/toastr.min.js')
- .pipe(gulp.dest(root_path.package_lib + '/toastr/'));
- gulp.src('./bower_components/angular/angular.min.js')
- .pipe(gulp.dest(root_path.package_lib + 'angular/'));
- gulp.src('./bower_components/angular-ui-router/release/angular-ui-router.min.js')
- .pipe(gulp.dest(root_path.package_lib + 'angular/'));
- });
- gulp.task("copy-all", ["copy-lib-css", "copy-lib-js"]);
- //Copy End
Now from the top menu in Visual Studio Goto > View > Other Window > Task Runner Explorer. Let’s get the task list by refreshing then run the task like the below screenshot.
As we can see the libraries have transferred to the root folder. We have used an admin template so the others libraries are listed in “js” folder.

We are now going to add the AngularJs library reference in the main layout page. Like below.
- <!DOCTYPE html>
- <html lang="en" ng-app="templating_app">
- <head></head>
- <body>
- <!-- Core JS Files -->
- <script src="/lib/jquery/jquery-1.10.2.js" type="text/javascript"></script>
- <script src="/lib/bootstrap/tether.min.js"></script>
- <script src="/lib/bootstrap/bootstrap.min.js" type="text/javascript"></script>
- <!-- App JS Files -->
- <script src="/lib/angular/angular.min.js"></script>
- <script src="/lib/angular/angular-ui-router.min.js"></script>
- </body>
- </html>
AngularJS Application
Let’s get started with AngularJS application, create a new folder name it “app”. The “app” folder will hold our all of frontend development files.
Let’s create all required file and folders. In shared folder, we have added partial views like sidebar/topbar menu that is going to call by an Angular directive.

Module
This will define our application.
- var templatingApp;
- (
- function () {
- 'use strict';
- templatingApp = angular.module('templating_app', ['ui.router']);
- }
- )();
Route
This file will handle our application route coming from URL’s.
- templatingApp.config(['$locationProvider', '$stateProvider', '$urlRouterProvider', '$urlMatcherFactoryProvider', '$compileProvider',
- function ($locationProvider, $stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider, $compileProvider) {
- //console.log('Appt.Main is now running')
- if (window.history && window.history.pushState) {
- $locationProvider.html5Mode({
- enabled: true,
- requireBase: true
- }).hashPrefix('!');
- };
- $urlMatcherFactoryProvider.strictMode(false);
- $compileProvider.debugInfoEnabled(false);
- $stateProvider
- .state('home', {
- url: '/',
- templateUrl: './views/home/home.html',
- controller: 'HomeController'
- })
- .state('dashboard', {
- url: '/dashboard',
- templateUrl: './views/home/home.html',
- controller: 'HomeController'
- })
- .state('user', {
- url: '/user',
- templateUrl: './views/user/user.html',
- controller: 'UserController'
- })
- .state('about', {
- url: '/about',
- templateUrl: './views/about/about.html',
- controller: 'AboutController'
- });
- $urlRouterProvider.otherwise('/home');
- }]);
Route Problem
This file will handle our application route coming from URLs. In AngularJs we need to enable HTML5 Mode to remove /#!/ symbols in the URL’’s below code snippet for enabling the mode.
- $locationProvider.html5Mode({
- enabled: true,
- requireBase: true
- }).hashPrefix('!');
Also, we need to specify the base in main page.
<base href="/">
Directives
For Top Navigation Bar
- templatingApp.directive("navbarMenu", function () {
- return {
- restrict: 'E',
- templateUrl: 'views/shared/navbar/nav.html'
- };
- });
For Top Side Bar
- templatingApp.directive("sidebarMenu", function () {
- return {
- restrict: 'E',
- templateUrl: 'views/shared/sidebar/menu.html'
- };
- });
Angular Controller
This is the Angular Controller which will manage the views and perform all HTTP calls from client end to server.








Tridip BhattacharjeePosted Jan 8, 2022, 7:39 AM
Good article.
sreenivasa kPosted Jan 27, 2020, 6:48 PM
Very good article on Single Page application UI. thanks for sharing
p tranPosted Dec 27, 2019, 6:00 PM
This is one of the best written articles . thank you
Zahid HasanPosted Jun 20, 2018, 1:14 AM
Very help full
SanPosted May 10, 2018, 5:11 AM
Nice Article ,In my case solution working fine after publish on IIS ,when we run the source code then facing the loading issues related to angular,jquery and some routing problem also.any idea how to resolved this issues
Rajesh GamiPosted Apr 16, 2018, 1:00 AM
Nicely explain sir