.NET Core MVC Interview Questions and Answers
- What is ASP.NET Core MVC?
- Answer: ASP.NET Core MVC is a lightweight, open-source framework for building web applications and APIs using the Model-View-Controller architectural pattern. It provides features like routing, model binding, dependency injection, and Razor view engine for creating dynamic web pages.
- Explain the MVC Pattern in ASP.NET Core MVC.
- Answer: MVC stands for Model-View-Controller:
- Model: Represents the application data and business logic.
- View: Renders the user interface and displays data to the user.
- Controller: Handles user input, updates the model, and selects the view to render.
- What are the main components of ASP.NET Core MVC?
- Answer: The main components are:
- Controllers: Handle incoming requests, process user input, and interact with the model.
- Views: Render UI and display data to the user.
- Models: Represent the application data and business logic.
- Middleware: Middleware components that handle HTTP requests and responses.
- Explain the request processing pipeline in ASP.NET Core MVC.
- Answer: In ASP.NET Core MVC, the request processing pipeline includes:
- Middleware: Pipeline starts with middleware that processes HTTP requests.
- Routing: Requests are routed to controllers based on URL patterns defined in routing middleware.
- Controller Execution: Controller actions process requests, execute business logic, and return responses.
- View Rendering: Views are rendered based on controller actions and data passed from the model.
- Describe the lifecycle of an ASP.NET Core MVC application.
- Answer: The lifecycle includes:
- Application Startup: Configuration and services are set up during application startup (Startup.cs).
- Request Handling: Requests are processed by middleware, routed to controllers, and handled by action methods.
- Response Generation: Views are rendered, and responses are generated to send back to clients.
- Application Shutdown: Cleanup and disposal of resources when the application shuts down.
- How does Dependency Injection work in ASP.NET Core MVC?
- Answer: ASP.NET Core uses built-in Dependency Injection (DI) to manage dependencies and promote loosely coupled components. Services are registered in
Startup.cs and injected into controllers and other classes that require them.
- Explain the startup process of an ASP.NET Core MVC application.
- Answer: During startup:
Program.cs: Main entry point where the host is built using WebHostBuilder.
Startup.cs: Configures services (using ConfigureServices method) and middleware (using Configure method).
- Middleware Pipeline: Middleware components are configured in the order they should handle requests.
- What is Razor View Engine in ASP.NET Core MVC?
- Answer: Razor is a syntax for creating dynamic web pages using C# or VB.NET. Razor views (.cshtml files) combine HTML markup with C# code to generate dynamic content. Views can access model data passed from controllers and include layout pages for consistent UI across pages.
- How does routing work in ASP.NET Core MVC?
- Answer: Routing maps incoming URLs to controller actions. It includes:
- Route Templates: Defined in
Startup.cs, these map URLs to controllers and actions.
- Attribute Routing: Allows defining routes directly on controller actions using attributes (
[Route]).
- Route Constraints: Specify requirements for route parameters (e.g., data type, regex patterns).
- Route Parameters: Capture values from the URL to pass to controller actions.
- What is ASP.NET Core Web API?
- Answer: ASP.NET Core Web API is a framework for building HTTP services that can reach a broad range of clients, including browsers and mobile devices. It's lightweight, supports JSON/XML responses, and is optimized for performance.
- Explain the differences between ASP.NET Core MVC and ASP.NET Core Web API.
- Answer:
- MVC: Used for building web applications with UI (views).
- Web API: Used for building RESTful services with HTTP endpoints that return data (JSON/XML) instead of views.
- Describe the lifecycle of an ASP.NET Core Web API application.
- Answer: The lifecycle includes:
- Application Startup: Configuration and services are set up during application startup (
Startup.cs).
- Request Handling: Requests are processed by middleware, routed to controllers, and handled by action methods.
- Response Generation: Data is serialized into JSON/XML format and returned to clients.
- Application Shutdown: Cleanup and disposal of resources when the application shuts down.
- How does routing work in ASP.NET Core Web API?
- Answer: Routing in Web API is similar to MVC:
- Route Templates: Defined in
Startup.cs, these map URLs to controller actions.
- Attribute Routing: Allows defining routes directly on controller actions using attributes (
[Route]).
- Route Constraints: Specify requirements for route parameters (e.g., data type, regex patterns).
- HTTP Methods: Routes are matched based on HTTP verbs (GET, POST, PUT, DELETE) to controller actions.
- Explain content negotiation in ASP.NET Core Web API.
- Answer: Content negotiation allows clients to request data in different formats (JSON, XML) using the
Accept header. ASP.NET Core Web API automatically serializes data into the requested format based on client preferences.
- How do you handle HTTP requests and responses in ASP.NET Core Web API?
- Answer: Controllers handle HTTP requests and return responses:
- HTTP Verbs: Controllers define methods (
GET, POST, PUT, DELETE) corresponding to HTTP verbs.
- Model Binding: Parameters from HTTP requests are bound to controller action parameters.
- ActionResult Types: Methods return
ActionResult<T> to provide HTTP status codes and data responses (e.g., Ok, BadRequest, NotFound).
- Discuss Dependency Injection in ASP.NET Core Web API.
- Answer: Web API uses built-in DI to inject services into controllers and other classes:
- Services Registration: Services are registered in
Startup.cs using ConfigureServices.
- Controller Injection: Services are injected into controllers using constructor injection (
ControllerBase).
- How do you secure ASP.NET Core Web API endpoints?
- Answer: Securing Web API includes:
- Authentication: Implementing authentication mechanisms like JWT (JSON Web Tokens), OAuth, or API keys.
- Authorization: Using policies and roles to restrict access to endpoints based on user roles or claims.
- HTTPS: Enforcing HTTPS to encrypt data transmission between clients and servers.
- Middleware: Adding security middleware like CORS (Cross-Origin Resource Sharing) and CSRF (Cross-Site Request Forgery) protection.
- Explain model validation in ASP.NET Core Web API.
- Answer: Model validation ensures incoming data meets specified rules before processing:
- Data Annotations: Decorating model properties with attributes (e.g.,
[Required], [Range]) to validate data.
- ModelState Validation: Checking
ModelState.IsValid in controllers to ensure data is valid before proceeding with business logic.
- Custom Validators: Implementing custom validation logic by extending
ValidationAttribute or using custom validation filters.