MSDN Documentation

.NET Web Fundamentals: API Reference

API Reference

This section provides a comprehensive reference to the core APIs used in .NET Web Fundamentals. Understanding these classes and methods is crucial for building robust and efficient web applications.

Core Classes and Interfaces

Microsoft.AspNetCore.Http.HttpRequest

Represents an incoming HTTP request. This object provides access to request details such as headers, query parameters, body, and more.

Properties:
Method
Gets the HTTP method (e.g., GET, POST, PUT, DELETE).
Path
Gets the path of the request URL.
Query
Gets the collection of query parameters.
Headers
Gets the collection of HTTP headers.
Body
Gets the request body as a stream.

Microsoft.AspNetCore.Http.HttpResponse

Represents an outgoing HTTP response. This object allows you to set status codes, headers, and write to the response body.

Properties:
StatusCode
Gets or sets the HTTP status code.
Headers
Gets the collection of HTTP response headers.
Body
Gets or sets the response body as a stream.
Methods:
Redirect(string url)
Sends a redirect response to the client.
WriteAsync(byte[] data)
Writes a byte array to the response body.

Microsoft.AspNetCore.Mvc.ControllerBase

The base class for all controllers in an ASP.NET Core application. Provides common functionality for handling HTTP requests and returning responses.

Common Methods:
Ok()
Returns an OK (200) response.
NotFound()
Returns a Not Found (404) response.
BadRequest()
Returns a Bad Request (400) response.
Ok(object value)
Returns an OK response with a value.

Microsoft.AspNetCore.Mvc.IActionResult

Represents the result of an action method. Implementations define how the action result is executed against the current HTTP context.

Common API Patterns

Request Handling

You typically access request information through the HttpContext object, which is available in controllers and middleware.


using Microsoft.AspNetCore.Http;

public class ExampleMiddleware
{
    private readonly RequestDelegate _next;

    public ExampleMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        var requestPath = context.Request.Path;
        var httpMethod = context.Request.Method;

        // Process request...
        await _next(context);
        // Process response...
    }
}
            

Response Generation

Controllers return implementations of IActionResult to define the response.


using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("[controller]")]
public class ItemsController : ControllerBase
{
    [HttpGet("{id}")]
    public IActionResult GetItem(int id)
    {
        if (id <= 0)
        {
            return BadRequest("Item ID must be positive.");
        }

        // Simulate fetching item...
        var item = new { Id = id, Name = $"Item {id}" };

        if (item == null)
        {
            return NotFound($"Item with ID {id} not found.");
        }

        return Ok(item); // Returns HTTP 200 OK with the item object
    }
}
            

Key Libraries and Namespaces

Further Reading