Single Page Application Using ASP.Net Core-AngularJs

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:


Before getting started make sure development environment is prepared properly. These are listed prerequisites to develop the sample application.

  1. Visual Studio 2017
  2. .NET Core 2.0
  3. NodeJS
  4. MSSQL 2016
  5. IIS

Key Points: The following point will be cared to develop the sample application, which is the key mechanism listed below.

  1. Manage Packages
  2. Transfer Libraries Using Gulp
  3. Working with AngularJs App
  4. Adding Middleware
  5. Scaffolding MSSQL Database
  6. Web-API
  7. Enable CORS
  8. Build AngularJS App Using Gulp
  9. Test Application (CRUD)
  10. Publish in IIS

Getting started

Let’s get started, open visual studio to create a new project, Goto > File > New > Project then choose 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 as following our requirements.

As our planning, 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 request then Angular will handle the request and decide which page to serve. Let’s create html page in 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”.

Middleware to Handle Client Side Routes Fallback: To avoid 404 error while reload the page in AngularJS SPA app we need to add middleware to handle client side route fallback.Below code snippet will take care of that.

Get more details on middleware here:

If we run our application to test, the static page will serve by default. Now let’s get started with client-end package managing.


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 project the click on “Manage Bower Packages

Install the required package by searching one by one. Finally the installed packages will 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 below screenshot.

Add all those packages to devDependencies, it’ll automatically install to our project.

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 below code snippet and paste it to the newly added file

Now from 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 below screeshot.

As we can see the libraries has 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.

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 angular directive.

Module: This will define our application.

Route: This file will handle our application route coming from URL’s.

Route Problem:

This file will handle our application route coming from URL’s. In AngularJs we need to enable HTML5 Mode to remove /#!/ symbols in URL’’s below code snippet for enabling the mode.

Also we need to specify the base in main page.


For Top Navigation Bar:

For Top Side Bar:

Angular Controller: This is the Angular Controller which will manage the views and perform all http call from client end to server.


Database: Let’s Create a Database in MSSQL Server. Here is the table where we are storing data.

In server-end we are going to generate EF models from existing database using reverse engineering.

Entity Framework: Before we use Entity Framework to our application we need to install packages.

Let’s right click on project then GoTo > Tools > NuGet Package Manager > Package Manager Console install below packages one by one.

  • Install-Package Microsoft.EntityFrameworkCore.SqlServer
  • Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
  • Install-Package Microsoft.EntityFrameworkCore.Tools.DotNet

After installation the .csproj file will look like this.

 NUGET Packages:

EF Model: Let’s run the following command using Package Manager Console.

dotnet ef dbcontext scaffold “Server=DESKTOP-80DEJMQ;Database=dbCore;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer –-output-dir Models

Now open the DbContext file then add a constructor to pass configuration like connectionstring into the DbContext.

Register DbContext: In Startup.cs let’s add our DbContext as service to enable database connection.

Enable CORS: To access the API’s from other domain we have enabled CORS. We have added the service in Startup.cs in ConfigureServices method.

CORS Mechanism:

Image Source:

API’s: Here’s our MVC API Controller using specific RoutePrefix attribute globally. With this api controller class we are performing database operation using Entity Framework DbContext.

We are almost done with application steps, now it’s time to minify our application in a single js file then reference it to the main html page.

 Modify Gulp: Add below code snippet to existing gulp file, Goto > TaskRunnerExplorer then refresh the window, it will work while we build the application due to its binding.

As we can see from below screenshot build is done successfully.

After build the application “wwwroot” folder will look like this.

Finally Main Html: let’s add the app.js file to the main html page.

OutPut: Here we can see user data is listed in grid, this data can also be accessible from other domain or devices.

Publish To IIS: Right Click on project file Goto > Publish window, choose a type to publish. I have choose folder publishing.

Now open IIS to add a new website

Goto Application Pool make sure about .Net CLR Version of 2.0 like below image.

Solve Underlying provider Issue in IIS:

Goto URL:

Let’s browse the website from IIS.

Source Code: I’ve uploaded the full source code to download/clone @github, Hope this will help 🙂