ASP.NET Core Interview Question #84

What are HttpRequest and HttpResponse?

Middleware & Request Pipeline Mid-Level Intermediate

Quick Interview Answer

HttpRequest contains information sent by the client, including the HTTP method, path, query string, headers, cookies, body, scheme, and host.

Detailed Explanation

HttpRequest contains information sent by the client, including the HTTP method, path, query string, headers, cookies, body, scheme, and host.

HttpResponse represents the server's response. It exposes the status code, headers, cookies, body, content type, and methods for writing response data.

Middleware can inspect both objects, but response headers normally cannot be changed after the response has started.

Code Example

app.MapGet("/request-info", (HttpContext context) =>
{
    var method = context.Request.Method;
    var host = context.Request.Host.Value;

    context.Response.Headers["X-Processed-By"] = "ASP.NET Core";

    return Results.Ok(new { method, host });
});