Create Angular 8 Application With ASP.NET Core 3.0

Introduction


Microsoft just released .NET Core 3.0 on 23rd September 2019. You can get the latest version from this URL. Please note, if you are a Windows developer, you must install Visual Studio 2019 version 16.3 or later to work with this version. .NET Core 3.0 contains more awesome features. Previous ASP.NET version 2.1 and 2.2 were shipped with Angular version 6 only. But in this current release, Microsoft added the latest version of Angular 8. They have resolved the loading issues in previous version as well. In this post, I will explain the steps to create an Angular 8 application with ASP.NET 3.0. We will create a simple controller class to get latest C# Corner posts details and will be shown in the home component. We will get more features soon from Microsoft soon.
 

Install Node.js latest version


Please download the latest version of Node.js from their website. You require Node.js 10 or later version to work with Angular 8 as per their official documentation.
 
 
 
Download and Install .NET Core 3.0 SDK.
 
As I mentioned earlier, .NET Core 3.0 requires Visual Studio 2019 v.16.3 or later. Please download and install the latest version of Visual Studio.
 
After installing both .NET Core 3.0 and Visual Studio 2019 v.16.3, you can create a new project in Visual Studio.
 
 
 
 
Choose ASP.NET Core Web Application template and click Next button.
 
 
 
Please give a valid name to the project and choose an appropriate folder to save that project. Click “Create” button to go next stage.  
 
 
 
Please note, by default HTTPS configuration is selected. If you do not wish that, please remove the tick from check box.
 
After clicking the “Create” button, your new project will be created shortly.
 
 
 
You can see a “ClientApp” folder is created in the project. This is our Angular 8 project.
 
 
 
 
 
If you check the package.json file, you can see the Angular library references. Please note that, node_modules are not yet created automatically. You must build the solution to create node packages. Other option is to just run the project. The node dependencies will be created automatically.  
 
 
 
 
 
In previous version of Angular 6 with ASP.NET Core 2.2, showed some error in the startup needed multiple refresh to resolve the issue. In this version, Microsoft resolved those issues.
 
 
 
 
 
But currently I am getting some errors in the console with default project. Hopefully, Microsoft team will resolve these issues in coming releases. Adding to this project, we can create a new controller for getting the latest posts details (articles, blogs and news) from C# Corner RSS feeds.
 

Create an API controller for getting C# Corner RSS Feeds


We can create a “Feed” class.
 
Feed.cs
  1. using System;  
  2.   
  3. namespace Angular8ASPNETCore3  
  4. {  
  5.     public class Feed  
  6.     {  
  7.         public string Link { getset; }  
  8.         public string Title { getset; }  
  9.         public string FeedType { getset; }  
  10.         public string Author { getset; }  
  11.         public string Content { getset; }  
  12.         public DateTime PubDate { getset; }  
  13.         public string PublishDate { getset; }  
  14.   
  15.         public Feed()  
  16.         {  
  17.             Link = "";  
  18.             Title = "";  
  19.             FeedType = "";  
  20.             Author = "";  
  21.             Content = "";  
  22.             PubDate = DateTime.Today;  
  23.             PublishDate = DateTime.Today.ToString("dd-MMM-yyyy");  
  24.         }  
  25.     }  
  26. }  
We can create the controller for getting RSS feeds for latest post details from C# Corner.
 
RssFeedsController.cs
  1. using Microsoft.AspNetCore.Mvc;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Globalization;  
  5. using System.Linq;  
  6. using System.Xml.Linq;  
  7.   
  8. namespace Angular8ASPNETCore3.Controllers  
  9. {  
  10.     [ApiController]  
  11.     [Route("[controller]")]  
  12.     public class RssFeedsController : ControllerBase  
  13.     {  
  14.         readonly CultureInfo culture = new CultureInfo("en-US");  
  15.   
  16.         [HttpGet]  
  17.         public IEnumerable<Feed> Get()  
  18.         {  
  19.             try  
  20.             {  
  21.                 XDocument doc = XDocument.Load("https://www.c-sharpcorner.com/rss/latestcontentall.aspx");  
  22.                 var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i => i.Name.LocalName == "item")  
  23.                               select new Feed  
  24.                               {  
  25.                                   Content = item.Elements().First(i => i.Name.LocalName == "description").Value,  
  26.                                   Link = (item.Elements().First(i => i.Name.LocalName == "link").Value).StartsWith("/") ? "https://www.c-sharpcorner.com" + item.Elements().First(i => i.Name.LocalName == "link").Value : item.Elements().First(i => i.Name.LocalName == "link").Value,  
  27.                                   PubDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture),  
  28.                                   PublishDate = Convert.ToDateTime(item.Elements().First(i => i.Name.LocalName == "pubDate").Value, culture).ToString("dd-MMM-yyyy"),  
  29.                                   Title = item.Elements().First(i => i.Name.LocalName == "title").Value,  
  30.                                   FeedType = (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("blog") ? "Blog" : (item.Elements().First(i => i.Name.LocalName == "link").Value).ToLowerInvariant().Contains("news") ? "News" : "Article",  
  31.                                   Author = item.Elements().First(i => i.Name.LocalName == "author").Value  
  32.                               };  
  33.   
  34.                 return entries.OrderByDescending(o => o.PubDate);  
  35.             }  
  36.             catch  
  37.             {  
  38.                 List<Feed> feeds = new List<Feed>();  
  39.                 Feed feed = new Feed();  
  40.                 feeds.Add(feed);  
  41.                 return feeds;  
  42.             }  
  43.         }  
  44.     }  
  45. }  
Please create above class under “Controllers” folder and paste above code. Please add required references to your class.
 
We can modify the Home component inside the “ClientApp”, “src” and “app” folder with below code.
 
home.component.ts
  1. import { Component, Inject } from '@angular/core';  
  2. import { HttpClient } from '@angular/common/http';  
  3.   
  4. @Component({  
  5.     selector: 'app-home',  
  6.     templateUrl: './home.component.html'  
  7. })  
  8. export class HomeComponent {  
  9.     latestPosts: Feed[] = [];  
  10.     constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {  
  11.         http.get<Feed[]>(baseUrl + 'rssfeeds').subscribe(result => {  
  12.             this.latestPosts = result;  
  13.         }, error => console.error(error));  
  14.     }  
  15. }  
  16.   
  17. interface Feed {  
  18.     link: string;  
  19.     title: string;  
  20.     feedType: string;  
  21.     author: string;  
  22.     content: string;  
  23.     publishDate: string;  
  24. }    
Also modify the template file with below code.
 
home.component.html
  1. <div style="text-align:center">  
  2.   <h1 style="margin-top:10px;">  
  3.     Welcome to Angular <b style="color:crimson">8</b> with ASP.NET Core <b style="color:navy">3.0</b>  
  4.   </h1>  
  5.   <img width="200" alt="Angular ASP.Net Core Logo" src="../assets/angularcore.png" />  
  6.   <div class="card" style="margin-left:50px; margin-right:50px; margin-bottom:50px;">  
  7.     <div class="card-header" style="font-weight:bold; font-size:x-large;">  
  8.       C# Corner Latest Posts from RSS Feeds  
  9.     </div>  
  10.     <div class="card-body">  
  11.       <div class="table-responsive" style="max-height:385px;">  
  12.         <table class="table mb-0" *ngIf="latestPosts && latestPosts.length>0">  
  13.           <thead>  
  14.             <tr>  
  15.               <th>Sl.No</th>  
  16.               <th>Post Title(With Link)</th>  
  17.               <th>Post Type</th>  
  18.               <th>Published Date</th>  
  19.               <th>Author</th>  
  20.             </tr>  
  21.           </thead>  
  22.           <tbody>  
  23.             <tr *ngFor="let post of latestPosts; let i = index">  
  24.               <td>{{ i + 1 }}</td>  
  25.               <td style="text-align:left;"><a href="{{post.link}}" target="_blank">{{post.title}}</a></td>  
  26.               <td>{{ post.feedType }}</td>  
  27.               <td>{{ post.publishDate }}</td>  
  28.               <td>{{ post.author}} </td>  
  29.             </tr>  
  30.           </tbody>  
  31.         </table>  
  32.       </div>  
  33.     </div>  
  34.   </div>  
  35. </div>  
 
We have successfully modified the Home component. We can run the application.
 
 
 
 
 
 
We could see the latest 30 articles/blogs/news details from C# Corner in this home page.

Conclusion


In this post, we could see how to create an Angular 8 project with ASP.NET Core 3.0 latest SDK in very simple steps. We could also see that; Microsoft improved the performance of Angular 8 application and reduced the initial loading time as well. I can say this is very light weight build. We can expect more exciting features from Microsoft team very soon. I will also try to write more articles on other features of .NET Core 3.0 very soon. Please give your valuable feedback about this post.


Similar Articles