Q1: What is ASP.NET Core, and how is it different from ASP.NET?
Answer:
ASP.NET Core is a modern, high-performance framework for building web applications and APIs. It is cross-platform and lightweight compared to ASP.NET, which is a part of the older .NET Framework and is only designed to run on Windows. ASP.NET Core also has improved performance, a simplified development model, and better support for cloud-based applications.
Q2: Explain the concept of middleware in ASP.NET Core.
Answer:
Middleware in ASP.NET Core refers to software components that are executed in a pipeline to handle requests and responses. Each middleware component can inspect or modify the request before passing it to the next middleware, or handle the response before sending it back to the client. Common examples of middleware include authentication, logging, and exception handling.
Q3: What is Dependency Injection, and how is it implemented in ASP.NET Core?
Answer:
Dependency Injection (DI) is a design pattern that allows a class to receive its dependencies from an external source rather than creating them internally. In ASP.NET Core, DI is built into the framework. You can register services in the ConfigureServices
method in Startup.cs
and inject them into controllers or other services using constructor injection.
Q4: How would you handle routing in ASP.NET Core?
Answer:
In ASP.NET Core, routing maps incoming HTTP requests to controllers and actions. Routing is handled by the UseRouting()
and UseEndpoints()
methods in the Configure
method of Startup.cs
. You define routes using attributes like [Route]
and [HttpGet]
on controller methods, or by using conventional routing patterns defined in the Configure
method.
Q5: Can you describe the process of creating a REST API using ASP.NET Core?
Answer:
To create a REST API in ASP.NET Core:
-
Create a new ASP.NET Core Web API project.
-
Define API controllers by creating classes derived from
ControllerBase
and decorating methods with HTTP verbs ([HttpGet]
,[HttpPost]
, etc.). -
Use
Route
attributes to define endpoint paths. -
Configure services like Entity Framework or other dependencies in
Startup.cs
. -
Run the application and test API endpoints using tools like Postman or Swagger.