ASP Core Interview Ques
ASP Core Interview Ques
What are the different hosting options available for ASP.NET Core
applications?
ASP.NET Core applications can be hosted in several environments,
including:
Kestrel, a cross-platform web server for ASP.NET Core.
IIS, as a reverse proxy server.
HTTP.sys, for Windows-based internet services without using IIS.
Docker containers, providing a way to package applications with
their dependencies and deploy them in a containerized
environment.
Cloud services like Azure App Service, which offers a fully managed
platform for building, deploying, and scaling web apps.
What are Data Transfer Objects (DTOs)? When and why would you
use them in an ASP.NET Core application?
DTOs are simple objects that are used to transfer data between processes
or layers in an application without unnecessary data or behavior. In
ASP.NET Core applications, you use DTOs to send only the required data
from the server to the client or vice versa, particularly when working with
APIs. This approach helps improve performance by reducing payload size
and ensuring that sensitive data is not exposed inadvertently. DTOs also
help in decoupling the internal domain model from the external interface,
making the system more robust to changes.
This filter logs messages before and after an action method executes.
What is middleware?
Middleware in the context of web applications, particularly with
frameworks like ASP.NET Core, is software that’s assembled into an
application pipeline to handle requests and responses. Each component in
the middleware chain is responsible for invoking the next component in
the sequence or short-circuiting the chain if necessary. Middleware
components can perform a variety of tasks, such as authentication,
routing, logging, and response compression.
What are the different types of tests you can write for ASP.NET
Core applications?
In ASP.NET Core applications, we can write unit tests, integration tests,
and functional tests. Unit tests focus on testing individual components or
methods for correctness. Integration tests verify the interaction between
components or systems, such as database access and API calls. Functional
tests, or end-to-end tests, validate the application as a whole, ensuring
that the user experience is as expected.
What is the Kestrel web server, and how does it differ from IIS?
Kestrel is a cross-platform web server for ASP.NET Core applications. It’s
lightweight and can be run directly from the command line or as a service.
Unlike IIS, which is a Windows-only web server and operates as a reverse
proxy, Kestrel can run as an edge server exposed to the internet or behind
a reverse proxy like IIS, Nginx, or Apache. Kestrel is designed to be fast
and supports full ASP.NET Core feature set.
What are static files in ASP.NET Core, and how do you serve
them?
Static files are files that are not processed by the server (like HTML, CSS,
JavaScript, and images). To serve static files in ASP.NET Core, use the
UseStaticFiles middleware in the Configure method of Startup.cs. This
middleware enables static files to be served from the web root (wwwroot)
directory by default, but you can also configure it to serve files from other
directories.
Explain the purpose and use of the Health Checks API in ASP.NET
Core.
The Health Checks API is used to check the health of an application and its
dependencies, such as databases and external services. It’s useful for
automated monitoring and readiness/liveness checks in microservices
architectures. You implement health checks by registering them in the
startup configuration and accessing them via a specified endpoint.
How do you deploy an ASP.NET Core application to a Linux
server?
Deployment typically involves:
Publishing the application from the development environment.
Transferring the published application to the Linux server.
Setting up a web server like Nginx or Apache as a reverse proxy to
forward requests to the Kestrel web server used by ASP.NET Core.
Configuring the server and application for production, including
environment variables, logging, and service management (e.g.,
using systemd).
What are some best practices for error handling and logging in
ASP.NET Core?
Global Exception Handling: Use middleware to handle global
exceptions, catch unhandled exceptions, and log them.
Use Logging Frameworks: Implement logging frameworks like
Serilog or NLog to log errors and application flow for diagnostics.
Structured Logging: Adopt structured logging to log complex data
types in a structured format, making it easier to query logs.
Error Handling Pages: Configure custom error pages for different
types of exceptions to provide a better user experience.