๐Ÿ“„

Notes on .NET Core Interview Questions Lecture

Jul 16, 2024

.NET Core Interview Questions Lecture

Introduction

  • 30 important questions for .NET Core interviews
  • Subscribe to the channel for more content

Basic Concepts

What is .NET Core?

  • New framework, open-source, developed by Microsoft
  • Different from .NET Framework and Mono for Xamarin
  • Shares common standard libraries (".NET Standard Library")

Advantages of .NET Core

  1. Cross-Platform: Runs on Windows, Linux, and Mac (unlike .NET Framework which runs only on Windows)
  2. Open Source: Free to use, modifiable, distributable
  3. Modern UI Integration: Easier integration with Angular, React
  4. Hosting: Can be hosted on multiple servers like Kestrel, Nginx
  5. Built-In Dependency Injection: Enables loosely coupled design
  6. Multiple IDE Support: Visual Studio, Visual Studio for Mac, Visual Studio Code

Project Structure in ASP.NET Core

Default Files and Directories

  1. wwwroot: Stores static files (JS, CSS, images)
  2. program.cs: Entry point of the application
  3. startup.cs: Configures services and request pipeline
  4. appsettings.json: Configuration settings (e.g., database connection strings)

Role of Files

program.cs

  • Entry point via Main() method
  • Builds the host server
  • Calls startup class via CreateHostBuilder method and Lambda expression

startup.cs

  • Configures services and request pipeline
  • Includes ConfigureServices and Configure methods

ConfigureServices and Configure Methods

ConfigureServices

  • Defines and adds services (e.g., AddControllersWithViews)
  • Optional based on request types

Configure

  • Configures the request pipeline
  • Mandatory methods in order
  • Middleware components (UseRouting, UseAuthorization, etc.)

Dependency Injection

Introduction

  • Creates loosely coupled design
  • Avoids tight coupling by using interfaces and dependency injection

Implementation

  1. Interface Creation: IStudent interface with method declaration
  2. Class Implementation: MathStudent and ScienceStudent implementing IStudent
  3. Service Registration: In startup.cs (AddSingleton<IStudent, MathStudent>())
  4. Controller Adjustment: Inject IStudent in controllers via constructor
  5. Switching Implementation: Change service registration in startup.cs to switch between MathStudent and ScienceStudent

Service Lifetimes

  1. Singleton: Single instance shared across application
  2. Scoped: Single instance shared per request
  3. Transient: New instance per request

Middleware in ASP.NET Core

What is Middleware?

  • Component executed on every request
  • Defined in Configure method as middleware components
  • Use: Adds to request pipeline, allows next middleware
  • Run: Terminates middleware execution
  • Map: Conditional middleware execution based on URL

Kestrel Web Server

  • Lightweight web server for ASP.NET Core
  • Supports cross-platform deployment

In-Process vs. Out-of-Process Hosting

  • In-Process: Uses IIS
  • Out-Of-Process: Supports Kestrel, IIS as reverse proxy

Hosting Configuration

  • Set ASPNETCORE_HOSTINGMODEL to InProcess or OutOfProcess
  • CreateHostBuilder builds and adds host

Configuration Settings

Techniques

  1. appsettings.json: Default technique
  2. Azure Key Vault: Used in Azure hosting
  3. Environment Variables
  4. In-Memory .NET Objects
  5. Command Line Arguments
  6. Custom Providers

Routing in ASP.NET Core

Conventional Routing

  • Handles HTTP requests based on URL pattern (Controller/Action/Parameter)
  • Defined in UseEndpoints method

Attribute Routing

  • Uses attributes to manipulate URL behavior
  • Route attribute to map URL to controller action

Request Processing Pipeline

  • Flow: HTTP request -> Middleware -> Routing -> Controller initialization -> Action method -> View result -> HTTP response

.NET Standard

  • A set of APIs implemented by platforms to ensure compliance

Razor Pages

  • Page-centric development model
  • Similar to ASP.NET WebForms

Dependency Injection in Views

  • Use @inject directive in views for injecting services

Custom Middleware

  • Create custom middleware class
  • Add custom logic in Invoke method
  • Register custom middleware in Configure method

State Management Techniques

  1. Cookies: Store data in the userโ€™s browser
  2. Session State: Transfer data between pages using cookies
  3. TempData: Transfer data from controller to view
  4. Query String: Append data to URL
  5. Hidden Fields: Process data within the same page during postbacks

Model Validation

  • Uses data annotations to enforce rules (e.g., Required attribute)
  • Define rules in model classes

Error Handling

  • Set error handling in Configure method
  • Different handling for development and production environment

Enabling Sessions

  • Service and middleware provided by Microsoft.AspNetCore.Session
  • Register AddSession in ConfigureServices
  • Use app.UseSession in Configure

Model Binding

  • Maps HTTP request data to controller action method parameters
  • Automatically converts data types (e.g., strings to integers)