ASP.NET Core API Documentation
Introduction to ASP.NET Core APIs
This section provides comprehensive documentation for the ASP.NET Core framework, covering its core components, APIs, and best practices for building modern, cloud-ready, internet-connected applications.
ASP.NET Core is a cross-platform, high-performance, open-source framework for building:
- Web applications
- Web APIs
- Microservices
- Real-time applications (e.g., with SignalR)
The framework is designed for high performance, modularity, and testability, enabling developers to build efficient and scalable applications.
Microsoft.AspNetCore.Mvc
This namespace contains the core classes for building Model-View-Controller (MVC) applications and APIs.
Key Classes and Interfaces
ControllerBase
: Base class for all controllers, providing fundamental MVC functionality.Controller
: Represents an MVC controller with support for view rendering.IActionResult
: Represents the result of an action method.[HttpGet]
,[HttpPost]
,[HttpPut]
,[HttpDelete]
: Attributes for defining HTTP method routing.[Route(...)]
: Attribute for defining routing templates.ModelStateDictionary
: Manages model state, including validation errors.ActionContext
: Provides context for action execution.
ControllerBase
The base class for controllers that handle requests. It offers essential features for API development, such as routing, model binding, and action execution.
Ok()
IActionResult Ok()
OkResult
- An OkResult
object that produces an HttpResponseMessage.StatusCode.OK
(200) response.
NotFound()
IActionResult NotFound()
NotFoundResult
- A NotFoundResult
object that produces an HttpResponseMessage.StatusCode.NotFound
(404) response.
Ok(object value)
IActionResult Ok(object value)
value
: The HTTP 200 OK response for an empty response body.
OkObjectResult
- An OkObjectResult
object that produces an HttpResponseMessage.StatusCode.OK
(200) response with a value.
Microsoft.AspNetCore.Http
This namespace provides types for handling HTTP requests and responses.
Key Classes and Interfaces
HttpContext
: Represents the context of an HTTP request.HttpRequest
: Represents an incoming HTTP request.HttpResponse
: Represents an outgoing HTTP response.RequestDelegate
: A delegate representing the application's request handling pipeline.
HttpRequest
Provides information about the incoming HTTP request.
Method
string Method { get; }
Gets the HTTP method of the request (e.g., "GET", "POST").
Path
PathString Path { get; set; }
Gets or sets the path of the URL for the request.
Query
IQueryCollection Query { get; }
Gets the query collection of the request.
HttpResponse
Provides functionality to construct an HTTP response.
StatusCode
int StatusCode { get; set; }
Gets or sets the status code of the response.
WriteAsync(string text)
Task WriteAsync(string text)
text
: The string to write to the response body.
Middleware
ASP.NET Core uses middleware to handle HTTP requests. Each piece of middleware has the opportunity to act on the request and then either pass it along to the next middleware or complete the response itself.
// Example of adding middleware to the pipeline
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello, World!");
});
});