Pages

Sunday, May 12, 2024

.Net 8 MVC

.Net Model View Controller (MVC) is a Server Side Web Application Technology. It Comprises of Model, which is the object oriented representation of data by way of POCO classes. View represents the user interface. Controller represents the user Interaction. There are other alternatives like Razor Pages for creating Server Side .Net applications. Mastering MVC will go a long way in learning other technologies like .Net Web API(with Controllers).
.Net MVC application can be developed either using Visual Studio, Visual Studio Code C# Dev Kit or .Net CLI. I am going develop MVC using Visual Studio.

Convention Over Configuration

Convention over Configuration is a software design pattern which denotes following certain conventions to avoid a lot of configurations. MVC used Convention Over Configuration when it comes to Controllers and Views. Controllers All Controllers should be inside the controllers folder. Controllers should have a Controller suffix. HomeController - Controller name is actually Home. View All the views should be inside the Views folder. There will be a folder in the Views folder corresponding the the Controller. Eg: There will be a Home folder inside the Views Folder for holding the views corresponding to Home Controllers action Methods.

MVC Project Structure

Controllers

User Interaction happens through Controllers. Controllers comprises of Action Methods.
 using Microsoft.AspNetCore.Mvc;  
 namespace MVCShalvin.Controllers  
 {  
   public class HomeController : Controller  
   {  
     public IActionResult Index()  
     {  
       return View();  
     }  
     public IActionResult Privacy()  
     {  
       return View();  
     }     
   }  
 }  
Here there are Index and Privacy Action Methods which returns View.

View

MVC View are created using Razor syntax. Razor comprises of C# code and HTML templates. Razor files have .cshtml extension Inside the Views Folder there is a Shared folder which contains Layout.cshtml which contains the common look and feel for the pages like Header, Footer etc. @RenderBody() section within the Layout.cshtml contains the View specific UI.

Models

 namespace MVCShalvin.Models  
 {  
   public class Faculty  
   {  
     public string Name { get; set; }  
     public string Specialization { get; set; }  
   }  
 }  

Passing data from Controller to View

 using Microsoft.AspNetCore.Mvc;  
 using MVCShalvin.Models;  
 namespace MVCShalvin.Controllers  
 {  
   public class HomeController : Controller  
   {  
     Faculty faculty = new Faculty  
     {  
       Name = "Shalvin P D",  
       Specialization = ".Net"  
     };  
     public IActionResult Index()  
     {  
       return View(faculty);  
     }  
   }  
 }  
Here the faculty object is passed as a parameter to View.

Index.cshtml

 @model MVCShalvin.Models.Faculty  
 <div>Hello @Model.Name specializing in @Model.Specialization</div>  

C# 12

Top Level Statement
 Console.WriteLine("Hello Shalvin P D");  
Memory Variable
 string name = "Shalvin P D";  
 Console.WriteLine("Hello, {0}", name);  
Interpolation
 string name = "Shalvin";  
 Console.WriteLine($"Hello {name}");  
Multiple Memory Variable
 string name = "Shalvin";  
 string location = "Vaduthala";  
 Console.WriteLine($"Hello {name}, located at {location}.");  
Memory Variable Console.ReadLine
 string name;  
 Console.WriteLine("Enter your Name: ");  
 name = Console.ReadLine();  
 Console.WriteLine($"Hello {name}");  
Multiple String Memory Variables
 string name, location;  
 Console.WriteLine("Enter your Name: ");  
 name = Console.ReadLine();  
 Console.WriteLine("Enter your Location: ");  
 location = Console.ReadLine();  
 Console.WriteLine($"Hello {name}, located at {location}.");  
Integer Memory Variable
 int i, j, res;  
 i = 23;  
 j = 45;  
 res = i + j;  
 Console.WriteLine(res);  
Int32.Parse (Simple Calculator)
 int i, j, res;  
 Console.WriteLine("Enter Value 1: ");  
   i = Int32.Parse(Console.ReadLine());  
 Console.WriteLine("Enter Value 2: ");  
   j = Int32.Parse(Console.ReadLine());  
 res = i + j;  
 Console.WriteLine(res);  
If Statement
 string city;  
 Console.WriteLine("Enter the Name of the City: ");  
 city = Console.ReadLine();  
 if (city == "Kochi")  
 {  
   Console.WriteLine("Industrial Capital of Kerala");  
 }  
If else
 string city;  
 Console.WriteLine("Enter the Name of the City: ");  
 city = Console.ReadLine();  
 if (city == "Kochi")  
 {  
   Console.WriteLine("Industrial Capital of Kerala");  
 }  
 else  
 {  
   Console.WriteLine("I don't know");  
 }  
Multiple else if
 string city;  
 Console.WriteLine("Enter the Name of the City: ");  
 city = Console.ReadLine();  
 if (city == "Kochi")  
 {  
   Console.WriteLine("Industrial Capital of Kerala");  
 }  
 else if (city == "Trichur")  
 {  
   Console.WriteLine("Cultural Capital of India");  
 }  
 else if (city == "Trivandrum")  
 {  
   Console.WriteLine("Capital of Kerala");  
 }  
 else  
 {  
   Console.WriteLine("I don't know");  
 }  

Thursday, March 28, 2024

Angular : Part 1 Getting Started with StackBlitz

Angular is a Single Page Application (SPA) framework from Google. Single Page Application is a web application that works entirely in the client side creating a rich user experience like that of Desktop application. Gmail is an example of SPA.

Setting a local development Angular requires installing NodeJS.

Easiest way to get started with Angular is Stackblitz. Stackblitz is an online editor.




For starting a new Angular project click the START A NEW APP option.