Click here to Skip to main content
15,666,180 members
Articles / Web Development / ASP.NET
Article
Posted 14 Jul 2022

Stats

6.9K views
57 downloads
12 bookmarked

Easy to Learn .NET 6.0 and Angular – Standalone Template and WEB API Publish to IIS – Part 5

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
14 Jul 2022CPOL11 min read
Getting started with .NET 6.0 and Standalone Angular Template with WEB API and publishing both WEB API and Angular projects to IIS
In this article, we will see in detail how to get started with .NET 6.0 and Standalone Angular Template with WEB API and Publishing both WEB API and Angular projects to the Internet Information Service.

Introduction

All this time, I planned to publish .NET 6.0 and Angular series of articles. In this series, I have planned to publish articles like:

In this article, we will see in detail how to get started with .NET 6.0 and Standalone Angular Template with WEB API and publishing both WEB API and Angular projects to the IIS (Internet Information Service).

This is a series of articles and in my previous article, I explained in detail about getting started with .NET 6.0 and Standalone Angular Template. In this article, let's see the steps to publish our .NET 6.0 StandAlone Template project and WEB API to IIS.

(WWW) -> What, Why and When We Need IIS

What is IIS: IIS is called as the Internet Information Service, which is a Web Server runs under the WINDOWS Operating System and introduced and created by Microsoft.

Why and When IIS: The main purpose of developing any web application is to store it in Web Server and view/access from anywhere in this globe via Internet. No matter if you stay in home, office or travelling if you have access to the internet, then you can view and access your developed web application from Web Server.

Image 1

Microsoft has developed the Internet Information Service which acts as a WEB Server in Windows Operating System.

Let’s consider as our office is in the same LAN (Local Area Network) and if we publish our ASP.NET Core Angular Application in IIS in our office server PC or in our local computer. Then within our office LAN network, we can view Web application from any computer, Notebook, Tablet or in Mobile which is in the same WIFI and in the same LAN network.

Image 2

The above image shows as I have published my demo .NET 6.0 WEB API and Angular Stand-Alone project to IIS. My Tablet, Notebook and Mobile phone is using the same WIFI connection and using the IIS PC IPADDRESS, we can view our web site from Mobile, Tablet and in Tablet. But here, my website is only accessible within the local AREA Network and if we want to access our website anywhere, even outside our office, then we can configure the global IP to our PC and then we can view our website anywhere.

The best way is as we need to have a WEB Server which we call as Production Server and we can publish our web site in Production server and make the Server access globally. We can also publish our Web Site in Cloud Server like Azure, AWS, etc.

Secure Your Website

As you can see, if we publish our website in local IIS, Production IIS Web Server or in Cloud Server, anyone can access the website from LAN or from WAN (Wide Area Network) Internet. If anyone knows our hosted PC IP Address, Port no or is aware about domain name, then we need to secure our website.

If our website has non important data like only information which can be open to all means, then we can make a simple website and make it accessible to all. If we want to secure our data and if we and only particular users want to see certain page and data means then for that website, we will add the features like Login, Authentication (check whether the user is valid or not to Login), Authorization (check the logged in user has permission to access the page and data), Session to make multiple user login and trace and track each session user activities in our website.

We can consider the Shopping cart websites which initially open to all the users as anyone can view all the products and details. But if they want to do online shopping, then they need to login and view their shopping lists and make new shopping here in the shopping site, we use login for the users by Authentication and Authorization to make access and able to view only the logged in users shopping list and the session is maintained for all the users individually to view only their shopping product listing details.

Bootstrap Website

Hope you are all aware of the word Bootstrap as we design and develop our web applications using the Bootstrap which is responsive CSS framework which makes developing our application faster and the web application can be viewed in any resolutions, so we can view our web application in Mobile, Tablet, Notebook, any computer and PDP’s. Our ASP.NET Core and Angular Templates are by default the Bootstrap added we can use it and design our website. Our previous article explained about .NET 6.0 and Angular using Admin LTE Design which is also designed using Bootstrap so our web application can be viewed in Mobile, Tablets, Computers and in PDPs.

Background

Installing IIS

First, check weather IIS is installed in your computer or not. To check that, open the browser and type localhost. If you see the page like this means that the IIS is already installed in your local computer.

Image 3

If the localhost is not displayed, it means then check and install the IIS to your computer.

Click Start > Enter Add or remove Programs > Click on Programs and Features

Image 4

Click Turn Windows Features on or off.

Image 5

Select the Internet Information Service and click OK.

Image 6

Install and open in Start > Enter IIS > Open the Internet Information Service.

Image 7

Now we are ready to publish our .NET 6.0 and Angular Application.

Prerequisites

Note: In order to host our ASP.NET Core in IIS, we must install the .NET Core Hosting Bundle. Kindly install it before hosting your ASP.NET Core applications to the IIS.

Using the Code

After installing all the prerequisites listed above and click Start >> Programs >> Visual Studio 2022 >> Visual Studio 2022 on your desktop, click New >> Project.

Image 8

Search for Angular and select Standalone Typescript Angular project and click Next.

Image 9

Enter your project name and click Next.

Image 10

If you want to use the ASP.NET Core and WEB API for your project, then you must check the Add integration for Empty ASP.NET Web API and click on Create button.

Image 11

Adding WEB API Project

Now let’s create a new ASP.NET Core WEB API project with our Angular Standalone Solution.

Right click your Angular Standalone Solution and add New Project > Select ASP.NET Core Web API and click Next.

Image 12

Enter the project name and make sure the project is under the Angular Standalone Solutions and click Next.

Image 13

We can see as the Framework here selected is .NET 6.0 and click on Create button.

Image 14

Now we can see as our ASP.NET Core WEB API project has been added under the Angular Standalone project solution.

Image 15

Disabling Launch Browser for WEB API Project

As we have both our Angular and WEB API project solutions in the same solutions, we do not need to open two browsers for Angular and WEB API. Our need is as our WEB API needs to be run in background and perform CRUD operations and some business operation in WEB API and all the data display and controls will be done from our UI Part in Angular project. So now, we disable the Launch Browser options for the WEB API Project.

Right click on our ASP.NET Core WEB API project and select Properties > select Debug > click on Open Debug Launch Profiles UI > uncheck Launch browser.

Image 16

Select the Startup Project

Now we need to make both our Angular and ASP.NET Core WEB API Project run, also we need to make the startup project to start initially. Here, first we need to run the WEB API project and followed by that, our Angular Project for making this.

Right click the Angular and ASP.NET Core WEB API Solution and select Properties, we can see by default it is selected as the Single startup project and here, we can see as our Angular Standalone project has been selected.

Image 17

In order to run both our Angular and ASP.NET Core project, we need to select the Multiple startup projects and select the action of each project as start. Make sure as your ASP.NET Core WEB API as top of Angular project as if we want to bind the ASP.NET Core results in Angular, then we need to run first our WEB API project and then Angular project.

Image 18

Open the Program.cs file and change and add the below code as to access the WEB API in both local and from the IIS Server.

C#
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment() || app.Environment.IsProduction())
{ 
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseCors();

Publishing WEB API to IIS

First let's publish the WEB API to IIS. For this, first let's create a folder under our C or D drive with WWROOT folder and inside, we create another folder named as AngularHosting and inside that, let's create a folder named as AngularAPI and AngularWeb as two folders.

We use the AngularAPI folder to build and publish all our WEB API project. In AngularWeb folder, we will deploy our Angular Project.

Image 19

Publish Angular API Project

Right click on our API Project and click on Publish > Select folder and click Next.

Image 20

Click Next and browse for the API publishing and click Finish.

Image 21

Click Next and browse for the API publishing and click Finish.

Image 22

We can see as we have successfully published the WEB API to the folder.

Image 23

Now we can open the web.config and will add the below code to allow cross origin for accessing our WEB API to be used in our Angular Application.

HTML
<httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
</httpProtocol>

Publish API to IIS

Click Start > Enter IIS > Right Click Sites > Click Add Website > Browse for the API deployed folder and enter the port no. which you need and click OK.

Image 24

Create New App Pool > and set the .NET CLR as No Managed Code.

Image 25

Select the website which created > In the right, click on Basic setting > change the APPPool to which you created.

Image 26

Browse and Test

Now we can check whether our API is running under IIS or not to check it from the Right Side > Click on Browse and we can see our API is running to test the swagger enter the /swagger and test it.

Image 27

We can also test it by our local Computer IP address with the port no.

Image 28

Publishing Angular App to IIS

Creating JSON file for API Connection

First let’s create an JSON file which we used to store the IIS API link address and we use the API address in our Angular project to bind the results.

For this, first I create a JSON file under our app folder and add the API Site IP address with port no.

Image 29

App.component

Now open the app.component file and first import the cons.json file ad next declare the variable as conns and assign the JSON data. In the http get method, we add the API iis ip and port address to get and bind the data.

TypeScript
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core'; 
import conString from './cons.json';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  public forecasts?: WeatherForecast[];
  conns: any = conString; 

  constructor(http: HttpClient) {
    http.get<WeatherForecast[]>
    (this.conns.ConnString+'weatherforecast').subscribe(result => {
      this.forecasts = result;
    }, error => console.error(error));
  }
  title = ‘ShanuAngular’;
}

interface WeatherForecast {
  date: string;
  temperatureC: number;
  temperatureF: number;
  summary: string;
}

Deploy the Angular Project

Now let’s deploy the Angular project to be published in IIS. Open the command prompt and go to the folder of your project and enter > ng build and click Enter from the command prompt.

Image 30

We can see as the files as been deployed and now copy the files to the folder where you want use it for the IIS publish.

Image 31

Follow the same method as we used to publish the WEB API and now, we create a new website and add the path with the Angular project and test it.

Image 32Image 33

Points of Interest

Hope this article helps you to understand getting started with ASP.NET Core and Angular Standalone projects and publish both API project and Angular Project in IIS.

History

  • 15th July, 2022: Initial version

License

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


Written By
Team Leader
India India
Microsoft MVP | Code Project MVP | CSharp Corner MVP | Author | Blogger and always happy to Share what he knows to others. MyBlog

My Interview on Microsoft TechNet Wiki Ninja Link

Comments and Discussions

 
-- There are no messages in this forum --