Click here to Skip to main content
15,885,216 members
Articles / Web Development / ASP.NET

Easy to Learn .NET 6.0 and Angular - Getting Started Angular Template - Part 1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
25 May 2022CPOL6 min read 27.8K   244   27   3
Get started with .NET 6.0 and Angular using the Angular Template available in VS 2022
This article details how to get started with .NET 6.0 and Angular using Angular Template that is available in Visual Studio 2022.

Introduction

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

In this article, we will see in detail how to getting started with .NET 6.0 and Angular using the Angular Template which is available in Visual Studio 2022.

.NET 6.0 has more improvement on the performance which includes a major advantage as it has the intelligent code editing and also it is called as the fastest full stack web framework. The .NET 6.0 also provides better performance in the File Stream.

When you create a new Angular Template project using Visual Studio 2022, you can notice the ASP.NET Core 6.0 and Angular 13 version have been used as both .NET6.0 and Angular 13 are new versions and surely, it will be quite interesting and fun to learn and work with new things.

Background

Prerequisites

  • Visual Studio 2022
  • Node.js – If you don’t have Node.js, install Node.js (download link)

Using the Code

Create ASP.NET Core with Angular Template

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

Image 1Image 2

Search for Angular Template and select ASP.NET Core with Angular and click Next.

Image 3

Enter your project name and click Next.

Image 4

Now we can see as the Framework is .NET 6.0 (Long term support). Click on the Create button .

Image 5

When we create the project, we can see the Solution structure like below:

Image 6

Here, we can see few interesting and new files have been added and we can also see as here in Visual Studio, there is no Startup.cs file which was the main class for the ASP.NET Core projects in 2019 versions.

Visual Studio 2019 with Startup.cs

If you had worked with the ASP.NET MVC and ASP.NET Core, you might be aware about the Startup.cs class. As this class is the very main class for all the .NET and .NET Core applications. This Startup.cs class contains configure and configureService methods. As this class is used to register all our services which our application needs in ConfigureService and the configure methods used as the middleware pipeline for controlling the application.

Image 7

Visual Studio 2022 does Not Need the Startup.cs

When we create ASP.NET Core 6.0 and Angular Application, the Startup.cs class file will be missing. Yes, now all the Configure and ConfigureService related all the service will be added in the Program.cs file.

For example, if we need to add the connection string and need to add the DBContext means then we can add it in the Program.cs file with builder.Service.AddDbContext(). To add all the services, we can use the builder object as we can see for adding the DBContext service, we use the builder.Service.AddDbContext().

Image 8

We can see that as there is no main method, class and using headers in the program.cs file, don’t panic, yes now in .NET 6.0, the codes are made simpler and the intelligent code supports seem to be more advanced and now it’s easier and reduce the time on working our codes and projects.

Top-Level Statements

This is called as the Top-Level statements as the main method is not needed from C# 9. The main aim of the Top-Level statement is to reduce the code and improve the performance. The main method and the class will be created by the compiler as we do not need to write separate code for it.

New Angular Files Part

aspnetcore-https.js

This JavaScript file sets up the HTTPS for the application which are targeted to sue the ASP.NET Core HTTPS certificates.

Image 9

proxy.conf.js

This JavaScript file is used to add our HTTPs ports, WEB API URL as target and in the context, we will be adding all our controller and methods to get access in the Angular application.

Image 10

Build and Run the Application

When we run the application, we can see by default the home. Counter and Fetch data components have been added in menu and we can navigate to each menu and see the component results.

Image 11

Current Month Item Wise Sales Details Making

Now let’s make a simple WEB API controller to get the Item Sales details.

Creating Model Class

For this, first we need to add the class file to create our Model class. Right click the project and click > Add > New Item. Select Class and name it as ItemDetails.cs and click Add.

Image 12

In the class, add property variables like below code:

C#
public class ItemDetails
    {
        public String ItemCode { get; set; }
        public int SaleCount { get; set; }
    }

Creating API Controller Class:

Right click Controllers folder and click > Add > Controller. Select API > Select API Controller – Empty and click Add:

Image 13

Give the empty API Controller name as ItemDetailsController.cs and click Add.

Image 14

In the controller, we add Get method which will create random 10 item code along with random Sale count value range from 20 to 100.

This is first article in this series. Later, when we use the SQL Server for CRUD, then we can see more in detail as how actually the real world examples work.

C#
[ApiController]
    [Route("[controller]")]
    public class ItemDetailsController : ControllerBase
    {
        private readonly ILogger<ItemDetailsController> _logger;
        public ItemDetailsController(ILogger<ItemDetailsController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetItemDetails")]
        public IEnumerable<ItemDetails> Get()
        {
            var rng = new Random();
            int ivale = 0;
            return Enumerable.Range(1, 10).Select(index => new ItemDetails
            {
                ItemCode = "ITM_" + rng.Next(1, 100),
                SaleCount = rng.Next(20, 100),
            }).ToArray(); ;
        }
    }

Angular Part

Here, I use the default home Angular components to update for showing the Item details with sale count.

home Component

In the home component, I have updated the code by adding the HTTPClient import part also Inject import part.

Then, I create the interface with ItemDetails as same we created in Model class.

In the Constructor method using the http.get method, we get the API JSON results and bind it to the itemsdetails to display the results in the HTML page.

JavaScript
import { Component, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
})
export class HomeComponent {
  public itemsdetails: ItemDetails[] = [];
  constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
    http.get<ItemDetails[]>(baseUrl + 'ItemDetails').subscribe(result => {
      this.itemsdetails = result;
    }, error => console.error(error));
  }
}
interface ItemDetails {
  itemCode: string;
  saleCount: number;
}

home.component.html

In the HTML, add the below code to display the results of ItemCode and salescount from API.

HTML
<h1>Current Month Item wise Sales Details</h1> 
<table class='table table-striped' aria-labelledby="tableLabel" *ngIf="itemsdetails">
  <thead>
    <tr>
      <th>Item Code</th>
      <th>Sales Count</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let itemdetail of itemsdetails">
      <td>{{ itemdetail.itemCode }}</td>
      <td>{{ itemdetail.saleCount }}</td>
    </tr>
  </tbody>
</table>

Build and Run

Build and run the application. In the Home page, we can see the result like below and you can notice as we are not able to see the itemdetails and salecount values. This is because we didn’t add yet the controller class and method in the proxy.config.js file. In order to use the WEB API in Angular App, we need to add the WEB API Controller and methods in Proxy.config.js context array.

Image 15

You can see as from the below image, now we have added our controller named as ItemDetails in the proxy.config.js file context array.

Image 16

Run the application to see the result. Now, we can see as our WEB API results have been binded in our home page with Item Code and Sales Count.

Image 17

Points of Interest

Hope this article helps you to understand how to get started with ASP.NET Core and Angular using Visual Studio 2022. In the next part, we will see more in detail how to use the ASP.NET Core for creating the Standalone Angular application.

History

  • 26th May, 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

 
QuestionDoes not work? Pin
trevormorris21-Aug-23 5:16
trevormorris21-Aug-23 5:16 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA26-May-22 2:54
professionalȘtefan-Mihai MOGA26-May-22 2:54 
Questionnice article Pin
Sobha Royal Crest26-May-22 2:47
Sobha Royal Crest26-May-22 2:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.