ASP.NET Core Overview
ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-based, and internet-connected applications. It provides a unified approach to web development by combining the features of MVC (Model-View-Controller) and Web API into a single framework.
Does .NET Hot Reload Support ASP.NET Core?
Yes, .NET Hot Reload supports ASP.NET Core. This feature allows developers to apply code changes to their running applications without needing to restart them. It helps speed up development by reducing downtime and improving the feedback loop.
Benefits of Using ASP.NET Core Over ASP.NET
- Cross-Platform: Runs on Windows, macOS, and Linux.
- High Performance: Designed to be lightweight and optimized for performance.
- Unified Framework: Combines MVC and Web API into a single programming model.
- Modular: Uses NuGet packages, allowing developers to include only the necessary components.
- Dependency Injection: Built-in support for dependency injection (DI), which improves testability and modularity.
- Cloud-Optimized: Ideal for building cloud-based applications with features like seamless integration with Azure.
- Open Source: ASP.NET Core is open source, allowing for community contributions and greater transparency.
Role of the Startup
Class
The Startup
class is responsible for configuring services and the application’s request pipeline in ASP.NET Core. It typically contains two main methods:
ConfigureServices
: Registers application services and sets up dependency injection.Configure
: Defines how the application will respond to HTTP requests by configuring the middleware pipeline.
Role of ConfigureServices
and Configure
Methods
ConfigureServices
: Used to add services to the application’s dependency injection container. These services can then be used throughout the application. Examples include configuring MVC services, adding logging, or setting up database connections.Configure
: Defines the middleware components that process HTTP requests. It specifies the order in which middleware is applied and can include custom logic for handling requests.
Describe Dependency Injection
Dependency Injection (DI) is a design pattern that enables a class to receive its dependencies from an external source rather than creating them itself. ASP.NET Core uses a built-in DI container that allows for easy injection of services into controllers, views, and other components.
Request Pipeline or Middleware Pipeline in ASP.NET Core
The request pipeline in ASP.NET Core is a series of middleware components that handle HTTP requests. Each middleware component can perform operations before and after passing the request to the next component. The pipeline is configured in the Startup.Configure
method.
Short-Circuiting the Request Pipeline in ASP.NET Core
Short-circuiting occurs when a middleware component ends the request processing without passing it to the next middleware in the pipeline. This is typically done for efficiency, such as when serving static files or handling errors.
Difference Between app.Run
and app.Use
app.Run
: Adds a terminal middleware component to the pipeline. It does not call the next middleware and ends the pipeline.app.Use
: Adds middleware that can call the next middleware in the pipeline, allowing for more complex request processing.
Problems Solved by Dependency Injection
- Decoupling: Reduces tight coupling between classes by using abstractions.
- Testability: Easier to test components by injecting mock dependencies.
- Maintainability: Simplifies updates to dependencies by changing the configuration in one place.
Describe the Service Lifetimes
- Transient: Created each time they are requested. Suitable for lightweight, stateless services.
- Scoped: Created once per request. Useful for services that maintain state within a request.
- Singleton: Created once and shared throughout the application’s lifetime.
Explain Middleware in ASP.NET Core
Middleware are components that form the request pipeline in ASP.NET Core. They process HTTP requests and can perform tasks such as authentication, logging, or modifying responses. Middleware is configured in the Startup.Configure
method using methods like app.Use
.
What is a Request Delegate?
A request delegate is a method that handles HTTP requests. It takes an HttpContext
object as input and performs operations on it. Delegates are used to define middleware components in the request pipeline.
What is a Host in ASP.NET Core?
The host is responsible for application startup and lifecycle management. It sets up the server, configuration, logging, and dependency injection. ASP.NET Core offers two types of hosts: Generic Host and Web Host.
Describe the Generic Host and Web Host
- Generic Host: A more general-purpose host for hosting any kind of app, not just web apps. It can be used for background services and console apps.
- Web Host: Specifically designed for web applications, setting up a web server (like Kestrel) and configuring ASP.NET Core-specific features.
Describe the Servers in ASP.NET Core
Servers in ASP.NET Core are responsible for handling HTTP requests. Common servers include:
- Kestrel: A cross-platform web server built into ASP.NET Core, used for high performance.
- IIS: Internet Information Services, a traditional Windows web server.
- Nginx: Often used as a reverse proxy server for ASP.NET Core applications running on Kestrel.
How Configuration Works in ASP.NET Core
Configuration in ASP.NET Core is provided by various sources like JSON files (appsettings.json
), environment variables, command-line arguments, and user secrets. Configuration is accessed via the IConfiguration
interface.
How to Read Values from Appsettings.json
File
Values can be read using the IConfiguration
interface, typically injected into classes via constructor injection. For example:
|
|
What is the Options Pattern in ASP.NET Core?
The Options Pattern is a way to group related configuration settings into classes and bind them to configuration sections. This makes it easier to manage and access settings throughout the application.
How to Use Multiple Environments in ASP.NET Core
ASP.NET Core supports different environments (e.g., Development, Staging, Production) using the ASPNETCORE_ENVIRONMENT
environment variable. You can create environment-specific configuration files (like appsettings.Development.json
) and use the IHostingEnvironment
interface to conditionally configure services.
How Logging Works in .NET Core and ASP.NET Core
ASP.NET Core provides built-in logging through the ILogger
interface. It supports logging to various providers like console, file, and third-party services. Logging levels (e.g., Information, Warning, Error) help filter log messages based on their importance.
How Routing Works in ASP.NET Core
Routing in ASP.NET Core maps incoming HTTP requests to route handlers, typically controllers and actions. It uses attribute-based routing or convention-based routing defined in the Startup.Configure
method using app.UseRouting
.
How to Handle Errors in ASP.NET Core
Error handling in ASP.NET Core is managed using middleware like app.UseExceptionHandler()
for centralized exception handling and app.UseStatusCodePages()
for custom status code responses.
How ASP.NET Core Serves Static Files
Static files like HTML, CSS, and JavaScript are served using the app.UseStaticFiles()
middleware. By default, it serves files from the wwwroot
directory.
Explain Session and State Management in ASP.NET Core
Session state management in ASP.NET Core is handled using ISession
to store and retrieve user-specific data across requests. Session state can be stored in-memory, distributed cache, or a database.
Can ASP.NET Application Be Run in Docker Containers?
Yes, ASP.NET Core applications can run in Docker containers. Docker provides a consistent environment for development, testing, and deployment by encapsulating applications and their dependencies.
How Does Model Binding Work in ASP.NET Core?
Model binding automatically maps HTTP request data to action method parameters. It binds form data, query strings, route values, and JSON payloads to controller action parameters.
Explain Custom Model Binding
Custom model binding allows developers to define their logic for binding incoming request data to action parameters. This is useful when default binding behavior does not meet specific needs.
Describe Model Validation
Model validation ensures that the data received from a request is valid according to specified rules (e.g., required fields, data types). Data annotations are commonly used to define validation rules, and validation errors can be checked using ModelState.IsValid
.
How to Write Custom ASP.NET Core Middleware
Custom middleware can be created by implementing a class with an Invoke
or InvokeAsync
method that takes an HttpContext
parameter. The middleware is then registered in the request pipeline using app.UseMiddleware<YourMiddleware>()
.
How to Access HttpContext
in ASP.NET Core
HttpContext
is accessed using dependency injection. In controllers, it is available through the ControllerBase.HttpContext
property. In other components, it can be injected via IHttpContextAccessor
.
Explain the Change Token
Change tokens provide a way to track changes in configuration or files. They are used to reload configuration settings without restarting the application. IOptionsMonitor
and IChangeToken
are commonly used for implementing change notifications.
How to Use ASP.NET Core APIs in a Class Library
ASP.NET Core APIs can be accessed in class libraries by referencing the appropriate NuGet packages (Microsoft.AspNetCore.Http
, Microsoft.Extensions.DependencyInjection
, etc.) and configuring dependency injection.
What is the Open Web Interface for .NET (OWIN)?
OWIN is a specification that defines a standard interface between web servers and web applications in .NET
. It decouples the web application from the server, providing flexibility in hosting environments.
Describe the URL Rewriting Middleware in ASP.NET Core
URL Rewriting Middleware is used to modify request URLs based on predefined rules. It is commonly used for SEO optimization, enforcing URL conventions, or migrating legacy URLs.
Describe the Application Model in ASP.NET Core
The application model is a collection of abstractions that define how controllers, actions, and other components interact within an ASP.NET Core application. It is used to customize and extend routing, action selection, and more.
Explain the Caching or Response Caching in ASP.NET Core
Response caching stores copies of responses to reduce the need to generate the same response multiple times. It can be implemented using response caching middleware (app.UseResponseCaching()
) and cache-related headers.
What is In-Memory Cache?
In-memory cache is a caching strategy that stores data in the server’s memory. It is suitable for small to medium-sized applications and scenarios where low latency and high performance are required.
What is Distributed Caching?
Distributed caching stores cache data across multiple servers, making it suitable for large-scale applications that require high availability and scalability. Redis and SQL Server are commonly used for distributed caching.
What is XSRF or CSRF? How to Prevent Cross-Site Request Forgery (XSRF/CSRF) Attacks in ASP.NET Core?
Cross-Site Request Forgery (CSRF) is an attack where a malicious site tricks users into performing actions on a different site where they are authenticated. ASP.NET Core provides built-in CSRF protection using anti-forgery tokens (@Html.AntiForgeryToken()
).
How to Prevent Cross-Site Scripting (XSS) in ASP.NET Core?
To prevent XSS attacks, ASP.NET Core provides features like automatic HTML encoding in Razor views and IHtmlHelper.Encode
. Input data should always be validated and encoded before displaying it in the UI.
What is an Area?
An Area is a way to organize large ASP.NET Core MVC applications into separate sections, each containing its controllers, views, and models. Areas help in managing the application’s structure and routes efficiently.
How to Enable Cross-Origin Requests (CORS) in ASP.NET Core?
CORS can be enabled using the app.UseCors()
middleware. It allows specifying which origins, methods, and headers are allowed in requests from other domains.
Explain Filters
Filters are used to run code before or after certain stages in the request pipeline. They can handle cross-cutting concerns like logging, authentication, and authorization. Common types include Authorization, Resource, Action, and Exception filters.
Describe the View Components in ASP.NET Core
View components are reusable components that encapsulate rendering logic. They are similar to partial views but can contain more complex logic and are not limited to being called from within a Razor view.
How View Compilation Works in ASP.NET Core
Views in ASP.NET Core are compiled at runtime by default. Precompilation can be enabled to compile views at build time, improving startup performance.
Explain Buffering and Streaming Approaches to Upload Files in ASP.NET Core
- Buffering: Stores the entire file in memory before processing. Suitable for small files but can lead to memory issues with large files.
- Streaming: Reads the file in small chunks, reducing memory usage and allowing large file uploads.
How Does Bundling and Minification Work in ASP.NET Core?
Bundling combines multiple files into a single file, while minification removes unnecessary characters (like whitespace) to reduce file size. These techniques improve loading times by reducing the number of HTTP requests and file sizes.
How Will You Improve Performance of ASP.NET Core Application?
- Enable response caching.
- Use distributed caching for large-scale applications.
- Minimize HTTP requests with bundling and minification.
- Optimize database queries and use asynchronous programming.
- Use CDN for static files.
Tools for Diagnosing Performance Issues in ASP.NET Core Application
- Application Insights: For detailed telemetry and performance monitoring.
- dotnet-counters: A performance monitoring tool for .NET Core applications.
- Profilers: Tools like Visual Studio Profiler, JetBrains dotTrace, or ANTS Performance Profiler.
- Logging: Implement structured logging to capture performance-related metrics.
Describe the ASP.NET Core MVC
ASP.NET Core MVC is a web development framework that implements the Model-View-Controller design pattern. It separates an application into three main components: Models (business logic), Views (UI), and Controllers (input handling).
Explain the Model, View, and Controller
- Model: Represents the application data and business logic.
- View: Defines how the UI is presented to the user. It displays data from the model.
- Controller: Handles user input, manipulates the model, and returns a view or action result.
Explain View-Model
A View-Model is a model specifically designed to contain the data required by a view. It helps in separating the view from the domain model, enhancing maintainability and testability.
Explain Strongly-Typed Views
Strongly-typed views are views that use a specific model type. They provide compile-time type checking and IntelliSense support in Razor syntax.
Describe Attribute-Based Routing
Attribute-based routing allows developers to define routes directly on controller actions using attributes (e.g., [Route("path")]
). It provides more control over URL patterns compared to convention-based routing.
Explain Dependency Injection for Controllers
Controllers can have dependencies injected into them via constructor injection. Services are registered in ConfigureServices
and automatically provided by the DI container.
How ASP.NET Core Supports Dependency Injection into Views
Dependency injection in views can be achieved by using the @inject
directive to inject services into Razor views.
How Will You Unit Test a Controller?
Unit testing a controller involves creating mock objects for dependencies (using tools like Moq), invoking controller actions, and asserting the expected results using test frameworks like xUnit or NUnit.
What is Cache Tag Helper in ASP.NET Core MVC?
The Cache Tag Helper caches the content inside it to improve performance. It can be configured based on various conditions like expiration time, query parameters, and user roles.
How Validation Works in MVC and How They Follow DRY Pattern
Validation is handled using data annotations on model properties. The [Required]
, [StringLength]
, and other attributes enforce validation rules. These attributes are shared between client-side and server-side validation, adhering to the DRY (Don’t Repeat Yourself) principle.
Describe the Complete Request Processing Pipeline for ASP.NET Core MVC
- Routing: Matches the incoming request URL to a route.
- Middleware: Processes the request through a series of middleware components.
- Controllers: Handles the request based on matched routes.
- Model Binding: Binds request data to action parameters.
- Action Execution: Executes the controller action.
- Result Execution: Returns an action result (e.g., view, JSON).
- Response: The result is sent back to the client.
.NET Core Overview
.NET Core is a cross-platform, high-performance, open-source framework used for building modern, cloud-based, and internet-connected applications. It is a modular framework that supports various application types like console, web, microservices, and more.
Latest Version of .NET Core
As of now, .NET 6 is the latest version of .NET Core, officially renamed to “.NET” to unify the platforms. A specific attribute of .NET 6 is long-term support (LTS), which offers security and bug fixes for an extended period.
Specific Features of .NET Core
- Cross-platform: Runs on Windows, macOS, and Linux.
- Modular and Lightweight: Applications only include necessary libraries.
- High Performance: Optimized runtime and memory usage.
- Unified Framework: Supports web, cloud, mobile, and desktop applications.
- Command-line Interface (CLI): Tools for development and deployment.
Uses of .NET Core
- Web applications using ASP.NET Core.
- Microservices architecture.
- Serverless applications.
- IoT applications.
- Console applications.
Critical Components in .NET Core
- CoreCLR: The runtime that executes the code and provides core services.
- CoreFX: A set of standard libraries providing APIs for various functionalities.
- ASP.NET Core: A framework for building web applications and services.
- Entity Framework Core: An Object-Relational Mapper (ORM) for database access.
- CLI (Command-Line Interface): Tools for project creation, compilation, and execution.
Difference Between .NET Core and Mono
- .NET Core: Cross-platform framework for building various application types, including cloud and server-side applications.
- Mono: Primarily designed for cross-platform mobile development, especially for iOS and Android using Xamarin.
.NET Core CoreFX
CoreFX is the foundational class library for .NET Core, providing a set of APIs for data structures, file handling, networking, and other core functionalities.
CoreCLR
CoreCLR is the runtime that runs applications built on .NET Core. It provides Just-In-Time (JIT) compilation, garbage collection, and low-level system functionality.
Difference Between .NET Core SDK and .NET Core Runtime
- SDK: Includes tools for building and compiling .NET Core applications, including the runtime, compilers, and libraries.
- Runtime: Only the necessary components to run a pre-built .NET Core application.
Where Not to Use .NET Core
- Applications that rely heavily on Windows-specific APIs.
- Applications requiring third-party libraries not compatible with .NET Core.
- Legacy applications tightly coupled to the .NET Framework.
Advantages of .NET Core
- Cross-platform compatibility.
- High performance and scalability.
- Open-source and community-driven.
- Modular architecture.
- Support for modern application development practices.
What is Kestrel?
Kestrel is a lightweight, high-performance web server included with ASP.NET Core. It can be used as a standalone server or behind a reverse proxy like Nginx or Apache.
.NET Core Middleware
Middleware are software components that handle HTTP requests and responses in the ASP.NET Core pipeline. Examples include authentication, error handling, and static file serving.
Razor Pages in .NET Core
Razor Pages is a page-based programming model that makes building web applications simpler by encapsulating the logic and view of a page into a single file.
Service Lifetimes in .NET Core
- Transient: Created each time they are requested.
- Scoped: Created once per request.
- Singleton: Created once and shared throughout the application’s lifetime.
Differences Between .NET Core and .NET Framework
- Platform: .NET Core is cross-platform; .NET Framework is Windows-only.
- App Models: .NET Core supports modern app models like microservices, whereas the .NET Framework supports traditional Windows apps.
- Performance: .NET Core is designed for high performance.
- API: .NET Core has fewer APIs but is expanding, whereas the .NET Framework has a mature and extensive API set.
Docker in .NET Core
Docker is used to containerize .NET Core applications, enabling them to run consistently across different environments. It helps in isolating dependencies and streamlining deployments.
What is .NET Core CI-1?
CI stands for Continuous Integration. CI-1 refers to a specific build or version in the continuous integration pipeline, indicating that it’s the first successful build after changes were integrated.
Hosting Environment Management
.NET Core provides mechanisms to manage different hosting environments (Development, Staging, Production). This allows for environment-specific configurations and behaviors.
Garbage Collection
Garbage Collection (GC) is an automatic memory management feature in .NET Core that frees up memory occupied by unused objects. It helps optimize memory usage and improve application performance.
CTS Types in .NET Core
Common Type System (CTS) defines the types and operations that .NET languages support, ensuring type safety and interoperability across languages. Examples include int
, string
, class
, and interface
.
Explain CoreRT
CoreRT is a project aiming to compile .NET Core applications to native code ahead of time (AOT) to improve startup time and reduce memory usage.
Importance of Startup Class
The Startup
class configures services and middleware for the application. It defines the request handling pipeline and application-wide services.
State Management
State management refers to preserving the state of an application across requests. Techniques include sessions, cookies, and query strings.
Best Way to Manage Errors in .NET Core
Use built-in exception handling middleware (app.UseExceptionHandler()
), logging frameworks, and custom error pages to manage errors effectively.
Is MEF Still Available in .NET Core?
The Managed Extensibility Framework (MEF) is not available in .NET Core out of the box. However, alternative dependency injection and modularity options are available.
Response Caching in .NET Core
Response caching stores copies of responses to serve future requests from the cache, reducing server load and improving response times.
What is a Generic Host in .NET Core?
The Generic Host is a system for configuring and running background services, including web apps and console apps. It sets up dependency injection, configuration, and logging.
Routing in .NET Core
Routing maps incoming requests to corresponding endpoints (e.g., controllers and actions) using patterns defined in the application.
Dependency Injection in .NET Core
Dependency Injection (DI) is a design pattern that allows for loose coupling and better testability by providing dependencies from an external source rather than creating them inside classes.
Role of IIS Manager for ASP.NET MVC
IIS Manager configures, manages, and deploys ASP.NET MVC applications in a Windows environment. It handles settings like application pools, authentication, and security.
Role-Based Authentication in ASP.NET MVC
Role-based authentication assigns users to roles (e.g., admin, user) and restricts access to resources based on those roles.
Difference Between ASP.NET and ASP.NET MVC
- ASP.NET: Web Forms-based approach, stateful, tightly coupled with server controls.
- ASP.NET MVC: Follows MVC pattern, stateless, more control over HTML and JavaScript, easier to test.
New Feature in ASP.NET Core MVC for Exposing Server-Side Code
Razor Pages is the new feature that provides a more straightforward way to expose server-side logic for rendering HTML.
View Component Feature
View components are reusable, self-contained components that render output. They are similar to partial views but can include more logic.
MVC Application Life Cycle
The MVC life cycle includes request routing, model binding, action execution, result execution, and rendering the view.
Different Return Types in MVC Controller Actions
- ViewResult: Renders a view.
- JsonResult: Returns JSON data.
- RedirectResult: Redirects to another action or URL.
- ContentResult: Returns plain text or HTML content.
Scaffolding in ASP.NET MVC
Scaffolding automatically generates code for CRUD operations based on the model, speeding up development.
Role of Action Filters
Action filters run code before or after an action method executes, handling concerns like logging, authentication, and validation.
Intercepting Exceptions in ASP.NET MVC
Use exception filters or the HandleErrorAttribute
to handle exceptions globally and provide custom error handling.
ASP.NET MVC and Its Components
ASP.NET MVC is a framework that separates application logic (Model), UI (View), and user input (Controller). It promotes separation of concerns and easier testing.
Advantages of ASP.NET MVC
- Separation of Concerns: Easier maintenance and testing.
- Full control over HTML: No ViewState or postbacks.
- SEO-friendly URLs: Better for search engine optimization.
- Testability: Supports unit testing.
Use of Areas in ASP.NET MVC
Areas organize large applications into smaller functional groupings, making them easier to manage.
Difference Between ViewData and ViewBag
- ViewData: Dictionary that uses string keys; requires typecasting.
- ViewBag: Dynamic property; more straightforward syntax without typecasting.
Request Flow in ASP.NET MVC Framework
- Routing: Matches request URL to controller and action.
- Controller: Executes action methods.
- Action Method: Interacts with model and returns a view.
- View: Renders HTML and returns it to the client.