Introduction
In my
previous articles, I talked about front-end frameworks with .NET Core 3. I decided to write an article on cross-platform which is suitable for IOS, Android, and Web, and that's why I chose
Ionic 4 Framework and its back-end as .NET Core 3.
What is Ionic Framework?
Ionic Framework is an open-source UI toolkit for building performant and high-quality mobile and desktop apps using web technologies (HTML, CSS, and JavaScript).
Ionic Framework is focused on the front-end user experience, or UI interaction of an app (controls, interactions, gestures, animations).
Currently, Ionic Framework has official integration with
Angular but support for
Vue and
React is in development.
Why do developers choose the Ionic Framework?
There are tremendous features that help developers to choose this platform, such as Ionic Framework is completely Free and Open Source. Ionic has a beautiful default UI like native apps, reusable codes, Angular in its base, a big and helpful developer community, plug-ins powered by Cordova, ease of testing, decreased app project costs, ability to reload to the browser is live. It's easier to move across platforms.
Configuration for .NET Core 3
Download the latest version of this SDK and install it.
Create an API project with .NET Core 3 (csharpcornerauthorionicapi). The important files like UserController.cs, User.cs, and Startup.cs should look like below respectively.
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.AspNetCore.Cors;
- using Microsoft.AspNetCore.Mvc;
- using Newtonsoft.Json;
-
- namespace csharpcornerauthorionicapi.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- [EnableCors("charppolicy")]
- public class UserController : ControllerBase
- {
-
- [HttpGet("{s}/{st}/{id}")]
- public ActionResult<IEnumerable<string>> Get(string s, string st = "", int id = 0)
- {
- return Ok(JsonConvert.SerializeObject(GetAllUser(s, st, id), Formatting.Indented));
- }
-
- public List<User> GetAllUser(string s, string st = "", int id = 0)
- {
- List<User> Author = new List<User>();
-
- if (s == null && id == 0)
- return Author;
-
- Author.Add(new User
- {
- Id = 1,
- role = "Admin",
- biodata = "C# Corner, Mindcracker",
- intro = "Startup Advisor, Speaker, Author, Architecture, Innovation",
- joineddate = "Oct 29 2004",
- location = "Philadelphia USA",
- name = "Mahesh Chand",
- profilelink = "https://www.c-sharpcorner.com/members/mahesh-chand",
- profilepicture = "https://csharpcorner-mindcrackerinc.netdna-ssl.com/UploadFile/AuthorImage/mahesh20160308020632.png"
- });
- Author.Add(new User
- {
- Id = 2,
- role = "Admin",
- biodata = "C# Corner Chief Editor and 3-times Microsoft MVP. I hold Masters degree in Computer Science and Applications and Bachelor’s degree in Mathematics. I am responsible for content publishing, product development, and migration of existing contents. I am also responsible for hiring new team members and managing the existing team",
- intro = "Not Available",
- joineddate = "May 16 2005",
- location = "Noida India",
- name = "Praveen Moosad",
- profilelink = "https://www.c-sharpcorner.com/members/praveen-moosad",
- profilepicture = "https://csharpcorner-mindcrackerinc.netdna-ssl.com/UploadFile/AuthorImage/prvn_13197120160516052234.jpg"
- });
- Author.Add(new User
- {
- Id = 3,
- role = "Editor",
- biodata = "Not Available",
- intro = "Editing and writing",
- joineddate = "Dec 30 2015",
- location = "USA",
- name = "TRIX MIDDLEKAUFF",
- profilelink = "https://www.c-sharpcorner.com/members/trix-middlekauff",
- profilepicture = "https://csharpcorner-mindcrackerinc.netdna-ssl.com/UploadFile/AuthorImage/26e38620180810040748.jpg"
- });
- Author.Add(new User
- {
- Id = 4,
- role = "Editor",
- biodata = "With an extensive hands-on experience of over 9 years, Pranav is working as Sr. Technical Editor in C# Corner team. He holds an MBA as well as a B-Tech degree in Information Technology. Here, he is responsible for editing and developing high-quality technical content, covering the news from the tech world, and editing all the day-to-day content being published on C# Corner, making it more readable and worth publishing.",
- intro = "Content curation, Copywriting, Copyediting,Tech writing, PHP/MySq",
- joineddate = "May 16 2016",
- location = "Delhi India",
- name = "PRANAV MITTAL",
- profilelink = "https://www.c-sharpcorner.com/members/pranav14",
- profilepicture = "https://csharpcorner-mindcrackerinc.netdna-ssl.com/UploadFile/AuthorImage/90108c20190521012831.jpg"
- });
- Author.Add(new User
- {
- Id = 5,
- role = "MVP",
- biodata = "Ankur Mistry is working as an Agile Coach, Certified Scrum Master (CSM), CSPO, He is C#Corner MVP (2018, 2017 and 2016), C#Corner Surat Chapter Lead, Speaker, CSP and Masters in Computer Application. Having 12 + years of experience in IT and his area of interest is C#, ASP.NET, SQL Server, Project Management, Scrum, Agile, crystal reports, MVC, WCF, Web Services, SSRS, HTML etc.",
- intro = "CSPO, CSP, CSM, C#, ASP.NET, SQL Server, crystal reports, MVC",
- joineddate = "Dec 19 2009",
- location = "Mumbai India",
- name = "ANKUR MISTRY",
- profilelink = "https://www.c-sharpcorner.com/members/ansh-mistry",
- profilepicture = "https://csharpcorner-mindcrackerinc.netdna-ssl.com/UploadFile/AuthorImage/ansh0603198220160402071949.jpg"
- });
- Author.Add(new User
- {
- Id = 6,
- role = "MVP",
- biodata = "Sr. SharePoint Developer working with Microsoft SharePoint (2010, 2013, 2016 and SharePoint Online), PowerApps, PowerBI, ASP.NET with C# and VB. Have hands on experience on Microsoft Azure. Love to remain on cutting edge of the technical knowledge",
- intro = "SharePoint , Office365, PowerApp, Microsoft Flow, Azure, PowerBI",
- joineddate = "Jul 29 2016",
- location = "Surat India",
- name = "AAKASH MAURYA",
- profilelink = "https://www.c-sharpcorner.com/members/aakash-morya2",
- profilepicture = "https://csharpcorner-mindcrackerinc.netdna-ssl.com/UploadFile/AuthorImage/d864b220180523093106.png"
- });
- Author.Add(new User
- {
- Id = 7,
- role = "Author",
- biodata = "A Software Engineer, experience with developing Web Application with the use of Microsoft technologies",
- intro = "AngularJS, ASP.NET, nopCommerce,ASP.NET CORE,React js",
- joineddate = "Jan 29 2019",
- location = "Surat India",
- name = "SMITEN KALATHIYA",
- profilepicture = "https://csharpcorner-mindcrackerinc.netdna-ssl.com/UploadFile/AuthorImage/01d00520190609091138.jpg",
- profilelink = "https://www.c-sharpcorner.com/members/smiten-kalathiya3"
- });
- if (!string.IsNullOrEmpty(s))
- Author = Author.Where(a => a.name.ToLower().Contains(s.ToLower())).ToList();
-
- if(!string.IsNullOrEmpty(st))
- Author = Author.Where(a => a.role.Contains(st)).ToList();
-
- if(id != 0)
- Author = Author.Where(a => a.Id == id).ToList();
-
- return Author;
-
- }
- }
- }
- namespace csharpcornerauthorionicapi
- {
- public class User
- {
- public int Id { get; set; }
- public string name { get; set; }
- public string location { get; set; }
- public string joineddate { get; set; }
- public string biodata { get; set; }
- public string intro { get; set; }
- public string profilepicture { get; set; }
- public string profilelink { get; set; }
- public string role { get; set; }
- }
- }
- services.AddCors(o => o.AddPolicy("charppolicy", builder =>
- {
- builder.AllowAnyOrigin()
- .AllowAnyMethod()
- .AllowAnyHeader();
- }));
-
- app.UseCors("charppolicy");
We are done with the API.
Configuration for Ionic 4
First, install
Node js (I recommend to use LTS version) and install Ionic with a single command using npm.
Let's understand with one example. My definition for this project is the C-sharp Corner member book.
There are three templates that are famous for the Ionic framework. You can choose anyone as per your requirement. In my case, I am using the highlighted one.
- ionic start csharpcornerauthor blank
- ionic start csharpcornerauthor tabs
- ionic start csharpcornerauthor sidemenu
Now, it requires 2 pages to display member names and member information and one service which would help us to connect between the API and Ionic 4. Run the below commands.
- ionic g page pages/author
- ionic g page pages/authordetail
- ionic g service services/author
Start with the service. Add the below code in the auther.service.ts file.
- import { Injectable } from '@angular/core';
- import { HttpClient } from '@angular/common/http';
- import { Observable } from 'rxjs';
- import { map } from 'rxjs/operators';
-
- export enum SearchType {
- all = '',
- admin = 'Admin',
- editor = 'Editor',
- mvp = 'MVP',
- author = 'Author'
- }
-
- @Injectable({
- providedIn: 'root'
- })
- export class AuthorService {
- url = 'https://localhost:44369/api/User/';
-
- constructor(private http: HttpClient) { }
-
- searchauthor(title: string, type: SearchType): Observable<any> {
- return this.http.get(`${this.url}?s=${encodeURI(title)}&st=${type}`).pipe(
- map(results => results)
- );
- }
-
- getauthorProfile(id) {
- return this.http.get(`${this.url}?id=${id}`);
- }
-
- }
Below is the code for auther.page.ts and auther.page.html respectively.
- import { Component, OnInit } from '@angular/core';
- import { AuthorService, SearchType } from './../../services/author.service';
- import { Observable } from 'rxjs';
-
- @Component({
- selector: 'app-author',
- templateUrl: './author.page.html',
- styleUrls: ['./author.page.scss'],
- })
- export class AuthorPage implements OnInit {
-
- results: Observable<any>;
- searchTerm: string = '';
- type: SearchType = SearchType.all;
-
- constructor(private authorservice: AuthorService) { }
-
- ngOnInit() { }
-
- livesearch() {
- this.results = this.authorservice.searchauthor(this.searchTerm, this.type);
- }
-
- }
- <ion-header>
- <ion-toolbar color="primary">
- <img src="https://csharpcorner-mindcrackerinc.netdna-ssl.com/App_Themes/CSharp/Images/SiteLogo.png">
- <ion-title style="margin-left: 142px;margin-top: -34px;">MEMBER BOOK</ion-title>
- </ion-toolbar>
- </ion-header>
-
- <ion-content>
-
- <ion-searchbar [(ngModel)]="searchTerm" (ionChange)="livesearch($event)"></ion-searchbar>
-
- <ion-item>
- <ion-label>Select Searchtype</ion-label>
- <ion-select [(ngModel)]="type" (ionChange)="livesearch($event)">
- <ion-select-option value="">All</ion-select-option>
- <ion-select-option value="Admin">Admin</ion-select-option>
- <ion-select-option value="Editor">Editor</ion-select-option>
- <ion-select-option value="MVP">MVPs</ion-select-option>
- <ion-select-option value="Author">Author</ion-select-option>
- </ion-select>
- </ion-item>
-
- <ion-list>
- <ion-item button *ngFor="let item of (results | async)" [routerLink]="['/', 'authors', item.id]">
- <ion-avatar slot="start">
- <img [src]="item.profilepicture">
- </ion-avatar>
- <ion-label text-wrap>
- <h3>{{ item.name }}</h3>
- <ion-icon name="locate"></ion-icon> {{ item.location }}
- </ion-label>
- <ion-icon slot="end" name="man"></ion-icon>{{item.role}}
- </ion-item>
-
- </ion-list>
-
- </ion-content>
For detailed page, we have to add the code for authordetail.page.ts and authordetail.page.html. It should look like below.
- import { Component, OnInit } from '@angular/core';
- import { AuthorService } from './../../services/author.service';
- import { ActivatedRoute } from '@angular/router';
-
- @Component({
- selector: 'app-authordetail',
- templateUrl: './authordetail.page.html',
- styleUrls: ['./authordetail.page.scss'],
- })
- export class AuthordetailPage implements OnInit {
-
- information = null;
-
- constructor(private activatedRoute: ActivatedRoute, private authorService: AuthorService) { }
-
- ngOnInit() {
-
- let id = this.activatedRoute.snapshot.paramMap.get('id');
-
- this.authorService.getauthorProfile(id).subscribe(result => {
- this.information = result;
- });
- }
-
- openWebsite() {
- window.open(this.information[0].profilelink, '_blank');
- }
-
- }
- <ion-header>
- <ion-toolbar color="primary">
- <ion-buttons slot="start">
- <ion-back-button defaultHref="/"></ion-back-button>
- </ion-buttons>
- <ion-title>c-sharp corner user profile detail</ion-title>
- </ion-toolbar>
- </ion-header>
-
- <ion-content padding>
-
- <ion-card *ngFor="let item of (information)">
- <ion-card-header>
-
-
- <ion-card-title>
- <ion-icon name="man"></ion-icon>
- {{ item.name }}
- </ion-card-title>
- <ion-card-subtitle class="subtitle">
- {{ item.intro }}
- </ion-card-subtitle>
- </ion-card-header>
- <ion-card-content text-center>
- <img [src]="item.profilepicture" class="info-img">
-
- <ion-item lines="none" style="text-align: center">
- <ion-label>{{item.location}}</ion-label>
- </ion-item>
-
- <ion-item >
- <ion-icon name="book" slot="start"></ion-icon>
-
- <ion-label text-wrap >{{ item.biodata }}</ion-label>
- </ion-item>
-
- <ion-item >
- <ion-icon name="calendar" slot="start"></ion-icon>
-
- <ion-label>{{item.joineddate}}</ion-label>
- </ion-item>
-
- <ion-item >
- <ion-icon name="albums" slot="start"></ion-icon>
- <ion-label text-wrap>{{item.role}}</ion-label>
- </ion-item>
-
- <ion-button expand="full" (click)="openWebsite()" >
- <ion-icon name="open" slot="start"></ion-icon>
- Profile Detail
- </ion-button>
- </ion-card-content>
- </ion-card>
-
- </ion-content>
Add HttpClientModule in the app.module.ts file.
- import {HttpClientModule} from '@angular/common/http';
- imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule,HttpClientModule]
The following is the screenshot of the App folder.
So far, we are done with the API project and created service with two pages. Service helps us to call the API and give appropriate data to the Ionic app. Also, we implemented a search functionality in it. Now, it's time to run the project. The command is here.
Finally, below is the output of this small application.
I hope you guys learned something new. Keep learning!
Here are the links to my previous articles.