Content

  • Introduction
  • Creating New Project
  • Directory Structure
  • Controller to View working
  • Models

Introduction

.NET Core is an open-source, cross-platform framework developed by Microsoft for building different types of applications, including web applications, console applications, microservices, libraries, and more. It's a modular and lightweight framework that runs on Windows, macOS, and Linux, making it highly versatile.

Features of .NET Core

  • Cross-Platform Development
  • Improved Performance compared to its predecessor, ASP.NET
  • Open Source
  • Community Support
  • Modern Development Features
  • Built-in Dependency Injection to achieve loose coupling
  • Security Features
  • Unified Programming Model

Core Components of .NET

  • Common Language Runtime (CLR): The Common Language Runtime (CLR) is the heart of the .NET Framework (including .NET Core and .NET 5+), providing various services such as memory management, exception handling, type safety, garbage collection, and thread management
  • .NET Class Library: A collection of reusable classes, interfaces, and value types that can be used to develop applications.
  • Common Type System (CTS): It provides a common set of data types ensuring that objects written in different .NET languages can interact with each other.
  • Common Language Specification (CLS): The Common Language Specification (CLS) in .NET is a set of rules and guidelines that defines a common set of features that all .NET languages should support. It ensures interoperability between different .NET languages, allowing code written in one language to seamlessly integrate with code written in another language within the same application.
  • ASP.NET: A part of the .NET framework that is used for building dynamic web applications and services.
  • Entity Framework: An Object-Relational Mapper (ORM) that enables .NET developers to work with relational data using domain-specific objects.
  • LINQ: Language-Integrated Query, which allows developers to write queries directly within the .NET programming languages.

Tools

  • Visual Studio: A powerful IDE from Microsoft for developing .NET applications.
  • Visual Studio Code: A lightweight, open-source editor also from Microsoft, suitable for .NET development with appropriate extensions.

Compilation

The C# compiler (csc.exe) compiles the source code into an intermediate form, known as Common Intermediate Language (CIL) or Microsoft Intermediate Language (MSIL). Alongside the CIL code, metadata describing the types, members, references, etc., in the code, is also generated.

Assembly

The CIL code and metadata are packaged together into an assembly. The assembly usually has a .dll (dynamic link library) or .exe (executable) extension, depending on whether it's a library or an application. An assembly contains a manifest, which has information about the assembly's version, culture, and other assembly metadata.

Code Execution

C# Source Code -> Assembly (.dll/.exe) -> .NET Runtime (CLR + JIT) -> Native Code (Platform Dependent)


Creating New Project

Open Visual Studio-> Create New Project -> ASP.NET Core Web App (Model-View-Controller) -> Enter Project Name | Select Location | Enter Solution Name -> Create


Directory Structure

  • Program.cs File : It contains application startup code - Dependency injection (services) | Middlewares | Host
  • wwwroot : It serves as the root directory for static files. These files typically include client-side assets like HTML, CSS, JavaScript, images, and other resources.
  • Controllers: Controllers play a critical role in handling incoming HTTP requests, processing user input, interacting with the model layer, and producing appropriate responses.
  • Views: Views are responsible for presenting the user interface and rendering HTML content,The "Views" folder typically contains subfolders corresponding to different controllers, "Shared" folder contains layout files
  • Models: It contains classes that represent the application's data model. Models encapsulate the business logic, data validation rules, and properties of the application's domain entities.
  • launchSettings.json: It is used to configure how the application is launched for debugging and development purposes. It specifies settings related to hosting, environment variables, and debugging options for different launch profiles.
  • appsettings.json It is used for storing configuration settings in a structured JSON format. These settings can include various parameters such as database connection strings, logging settings, API keys, feature toggles, and other application-specific configuration options.

Controller to View working

We can omplement communication between controller to View using @model directive

Example:

Controller File

public IActionResult Index()
{
    int a = 70;
    return View(a);

    //to pass string
    //string b = "test";
    //return View("Index",b);
}

View File

@model int
@* @model string *@                   
@{
ViewData["Title"] = "Home Page";
}
                    
<div class="text-center">
   <h1 class="display-4">Welcome</h1>
   <h2>Value from Controller - @Model</h2>
   <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

Creating Models

  • Model define the structure and behavior of the data entities, including properties, methods, validation rules, and relationships.
  • Models encapsulate data access logic, such as querying and updating data in the database, and may interact with services or repositories to perform CRUD (Create, Read, Update, Delete) operations.

Example:

public class Students
{
    public int Id { get; set; }
            
    public string Name { get; set; } = "Test";
            
    public string City { get; set; } = "Test";
}

Example : Using Model in Controller

Contoller File

public IActionResult Index()
{
    Students student = new Students();
    student.Id = 1;
    student.Name = "Ram";
    student.City = "Pune";
    return View(student);
}

View File

@model Students
@* @model string *@
@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <h2>Value from Controller</h2>
    <ul>
        <li>Student Id - @Model.Id</li>
        <li>Student Name - @Model.Name</li>
        <li>Student City - @Model.City</li>
    </ul>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>