Full-stack application development with AngularJS 11 and Asp.Net MVC Core 5.0 — Creating Combined Project [part1/4]

This is the first article of the series I am going to write for a complete full-stack application using AngularJS as front-end and Asp.Net MVC Core as back-end. This series will be a comprehensive tour of features of both technologies and we will end up with a complete full-stack project. Below are links to Each Part:

  1. Creating and connecting Angular and Asp.Net MVC Core Projects
  2. Creating data models to ClientApp and ServerApp
  3. Creating and consuming Web Services
  4. Generic Repositories and Controllers

This article is about using Angular and ASP.NET Core MVC together to create rich applications. Individually, each of these frameworks is powerful and feature-rich, but using them together combines the dynamic flexibility of Angular with the solid infrastructure of ASP.NET Core MVC.

Before you start

https://dotnet.microsoft.com/download

https://nodejs.org/en/

https://visualstudio.microsoft.com/vs/

https://code.visualstudio.com/

https://www.microsoft.com/en-in/sql-server/sql-server-downloads

Creating the Project

There are several different ways to create a project that combines Angular and ASP.NET Core MVC. The approach that I use in this article relies on the @angular/cli package, used in conjunction with the .NET tools for creating a new MVC project.

Go to Windows PowerShell and execute the following command:

angular/cli 11.0.3 installed

angular/cli@11.0.3 has been successfully installed.

Creating the Angular Part of the Project

Open the new PowerShell prompt and locate the convenient location and run the following command to create an angular app:

When the setup is complete, the result is a folder called FullStackApp/ClientApp that contains the tools and configuration files for an Angular project, along with some placeholder code to help jump-start development and check that the development tools are working.

It can take a moment for the development tools to start and compile the project for its first use. Once the “Compiled successfully” message is shown, open a new browser window and navigate to http:// localhost:4200 to see the placeholder content that is added to new Angular projects

Creating the ASP.NET Core MVC Part of the Project

Once the Angular project has been set up, the next step is to create an ASP.NET Core project. Use a PowerShell command prompt to run the commands shown below in the FullStackApp folder.

The dotnet new command adds all the files required for a basic ASP.NET Core MVC project to the FullStackApp/ServerApp folder, alongside the Angular project:

1) Goto > Open a project or solution

2) Navigate to the FullStackApp/ServerApp folder, and select the ServerApp.csproj file

3) Visual Studio will open the project in .NET mode as shown below:

4) Right-click the Solution item at the top of the Solution Explorer window and select Add > Existing Web Site from the popup menu.

5) Navigate to the FullStackApp folder, select the ClientApp folder, and click the Open button.

Visual Studio will add the ClientApp folder to the Solution Explorer so that you can see the contents of the Angular project, as shown below:

Solution explorer after opening both projects will look like as shown below:

Right-click the ClientApp item, select Property Pages from the popup menu, and navigate to the Build section. Make sure that the “Build Web site as part of the solution” option is unchecked, as shown in Figure below:

Select File > Save All; Visual Studio will prompt you to save the solution file, which can be used to open the Angular and ASP.NET Core MVC projects when you want to pick up a development session. Save the file in the FullStackApp folder using the name FullStackApp.sln.

When you need to open the project again, open the FullStackApp.sln file; both parts of the project will be opened and displayed in the Solution Explorer window.

The final look of the FullStackApp folder will be as follows:

To configure the ports used when the application is started using Visual Studio, make the changes shown in the figure below to the launchSettings.json file in the ServerApp/Properties folder.

The final preparatory step is to regenerate the development HTTPS certificates by running the commands shown below:

Select the YES option for each prompt that Windows presents.

The ASP.NET Core MVC part of the project can be compiled and executed from the command line or using the code editor.

To build and run the project from the command line, run the command shown in the figure below in the ServerApp folder:

Open a browser window and navigate to https://localhost:5001; you will see the placeholder content shown in Figure below.

Connecting the Angular and ASP.NET Core Applications

The Angular and ASP.NET Core MVC applications share the same parent folder but are not connected in any way. It is possible to develop applications this way, but it is awkward. A more useful approach is to connect the two toolchains so that HTTP requests are received by ASP.NET Core and passed on to either the MVC framework or the Angular development tools based on the request URL.

There are two ways to connect the toolchains, and each is useful during a different phase of the project lifecycle:

  1. Managing the Angular Server Through ASP.NET Core
  2. Using the ASP.NET Core MVC Proxy Feature

Both approaches rely on an additional .NET package. Open a new PowerShell command prompt, navigate to the FullStackApp/ServerApp folder, and execute the following command:

You will find the highlighted package added Packages of ServerApp

This technique is to start the Angular development tools separately from the ASP.NET Core runtime. In this approach, restarts are faster and each part of the project responds independently so that a change to a C# class, for example, doesn’t affect the Angular development server. The drawback of this approach is that you need to run two commands and monitor two streams of output to see messages and errors.

Add the statements shown below to the Startup class to configure ASP.NET Core MVC to forward requests to the Angular development server, along with statements that select the connection technique based on configuration settings.

Notes: Do not forget to use the following directive:

Adding Configuration Settings in the appsettings.Development.json File in the ServerApp Folder

Starting the Angular Development Server

To start the Angular development server, open a new PowerShell command prompt, navigate to the FullStackApp/ClientApp folder:

Starting the ASP.NET Core Server

To start the ASP.NET Core server, open a second PowerShell prompt, navigate to the FullStackApp/ServerApp folder, and run the following command :

Open both applications on browsers

Open a new browser and navigate to https://localhost:5001 for ServerApp.

and then https://localhost:5001/app for ClientApp

Summary

We saw how to create a project that combines Angular and ASP.NET Core MVC. The process is a little complicated, but the result is a solid foundation that allows the Angular and MVC parts of an application to work together while preserving the toolchain used by each of them.

The full code of this article can be downloaded from the Github repository.

https://github.com/gulraizgulshan2k18/angular-mvc-fullstack

Artificial Intelligence Enthusiast, SDN Developer, Virtualization Expert

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store