In ASP.NET Core Minimal APIs, what is the difference between Results.Ok and TypedResults.Ok, and why is TypedResults usually the better choice?
answer
- same response at run time
- IResult vs Ok<T> as declared type
- IEndpointMetadataProvider.PopulateMetadata
- metadata only from the declared type
- 10: 401/403 instead of cookie redirect
basics
~20 sBoth write the same 200 JSON response; Results.Ok is declared to return IResult while TypedResults.Ok returns Ok<T>, whose static metadata documents the response, makes tests cast-free and, in ASP.NET Core 10, stops cookie authentication redirecting.
solid answer
~40 sAt run time they are identical: every `Results` helper simply calls the matching `TypedResults` helper. The difference is the **declared** type. `Results.Ok(order)` is typed as `IResult`, and when a handler's return type is `IResult` ASP.NET Core adds no response metadata, because it cannot know what the result will do. `TypedResults.Ok(order)` is typed as `Ok<Order>`, which implements `IEndpointMetadataProvider`; when the endpoint is built, its static `PopulateMetadata` adds a 200 `Order` `application/json` response, so the OpenAPI document is right without `Produces`. The concrete type also exposes `Value` and `StatusCode` to unit tests without casting. In ASP.NET Core 10 most typed results also add metadata that makes cookie authentication answer 401 or 403 instead of redirecting to a login page. The cost is that handlers with several outcomes must declare a `Results<...>` union.
code
csharp · 9 lines// IResult-typed: correct 200 response, but no response metadata;
// under cookie auth in 10, still redirects to the login page
app.MapGet("/returns/{rmaId}", (int rmaId, IReturnsStore store) =>
Results.Ok(store.Get(rmaId)));
// Ok<ReturnRequest>-typed: 200 + ReturnRequest + application/json in metadata,
// and cookie auth answers 401/403
app.MapGet("/v2/returns/{rmaId}", (int rmaId, IReturnsStore store) =>
TypedResults.Ok(store.Get(rmaId)));go deeper
Know that TypedResults returns concrete types like Ok<T> while Results returns IResult, and that both send the same response.
Explain how IEndpointMetadataProvider on the declared return type feeds OpenAPI, and why IResult-typed handlers lose that.
Enforce typed returns so the published contract cannot drift, and know the ASP.NET Core 10 cookie-auth consequence of IResult-typed endpoints.
Decide whether typed results become a review rule for every public endpoint, weighing union-type ceremony against contract accuracy.
## Two factories, one set of results `Results` and `TypedResults` are static classes in `Microsoft.AspNetCore.Http` with nearly the same methods: `Ok`, `Created`, `Accepted`, `NoContent`, `NotFound`, `Conflict`, `BadRequest` and more. They produce the same objects. In the ASP.NET Core 10 source, `Results.Ok(value)` is a one-liner that returns `TypedResults.Ok()` when the value is null and `TypedResults.Ok(value)` otherwise. What differs is the **signature**: | | `Results.Ok(order)` | `TypedResults.Ok(order)` | |---|---|---| | Declared return type | `IResult` | `Ok<Order>` | | Runtime response | 200, JSON body | 200, JSON body | | Endpoint metadata when the handler's return type is that type | none | 200, `Order`, `application/json` | | Unit test access | cast to `Ok<Order>` first | `result.Value`, `result.StatusCode` | | Several outcomes in one lambda | compiles, all are `IResult` | needs `Results<...>` | ## Where the metadata comes from When `RequestDelegateFactory` builds an endpoint, it inspects the handler's declared return type (unwrapping `Task<T>`): 1. For `void` or any `IResult` type it adds **no built-in response metadata**; an `IResult` could write anything. 2. If the declared type implements **`IEndpointMetadataProvider`** (namespace `Microsoft.AspNetCore.Http.Metadata`), it calls the type's `static abstract void PopulateMetadata(MethodInfo method, EndpointBuilder builder)`. 3. `Ok<TValue>` implements it by adding a `ProducesResponseTypeMetadata` for status 200, type `TValue` and `application/json`. `NotFound` adds 404, `Conflict<TValue>` adds 409 with the error type, `NoContent` adds 204. The OpenAPI document generator and any other `ApiExplorer` consumer read that metadata. A handler declared as `IResult`, whether it uses `Results.Ok` or even `TypedResults.Ok`, gives them nothing, and you are back to `.Produces<Order>(200)` calls that can drift from the code. Not every typed result describes a status. `TypedResults.StatusCode(409)` returns `StatusCodeHttpResult`, `TypedResults.Text(...)` returns `ContentHttpResult`, and neither implements `IEndpointMetadataProvider`; `JsonHttpResult<T>` from `TypedResults.Json(...)` implements it but adds no response type, since its status is chosen at run time. Prefer the named factories (`Conflict`, `NotFound`) when the status is fixed. ## What changed in 9 and 10 - **ASP.NET Core 9** added `TypedResults.InternalServerError()` and `InternalServerError<TValue>`, with a matching 500 metadata entry. - **ASP.NET Core 10** changed cookie authentication: for an endpoint carrying `IDisableCookieRedirectMetadata`, an unauthenticated request gets **401** and a forbidden one **403** instead of a redirect to the login or access-denied page. Most typed results (`Ok<T>`, `Created`, `NoContent`, `Conflict` and others) add that metadata in `PopulateMetadata`, as do endpoints that read a JSON body or return a plain JSON object. A GET handler declared to return `IResult` gets none of it, so under cookie authentication it **still redirects**. The 10.0 docs call the marker `IApiEndpointMetadata`; the shipped interface is `IDisableCookieRedirectMetadata`. ## When Results is still acceptable - **Prototypes and internal tools** where nobody reads the OpenAPI document and no cookie authentication is involved. - **Handlers with many dynamic outcomes** that are documented explicitly anyway; the metadata would come from `Produces` calls either way. - **Code that only needs an `IResult` value**, such as a helper that maps a domain error to a response at run time. Even there, returning the concrete typed result from the helper keeps the option of declaring it. Switching later is cheap: the factory names match one to one, so moving from `Results` to `TypedResults` is mostly a rename plus declaring union return types where a handler has more than one outcome. ## Living with the stricter types - A lambda with two outcomes must declare `Results<Ok<Order>, NotFound>` as its return type, or move into a method. - `async` handlers return `Task<Results<...>>`. - `TypedResults` helpers can still be used inside an `IResult`-typed handler; you get correct runtime behaviour but lose the metadata. - Unit tests can assert `Assert.IsType<Ok<Order>>(result)` and read `Value` directly. ## What interviewers listen for - That the two produce the **same response**; the difference is static type information. - That metadata flows from the **declared return type** through `IEndpointMetadataProvider`. - The trade-off: precise documentation and tests versus the `Results<...>` ceremony. - For ASP.NET Core 10, the cookie-redirect consequence.
- If a handler uses TypedResults.Ok but declares IResult as its return type, does the endpoint get the metadata?No. The request delegate factory reads the handler's declared return type, after unwrapping `Task<T>`, to decide whether to call `PopulateMetadata`. `IResult` does not implement `IEndpointMetadataProvider`, so nothing is added even though the runtime object is an `Ok<T>`. Declare `Ok<T>` or a `Results<...>` union to keep it.
- Why does TypedResults.StatusCode(409) document less than TypedResults.Conflict()?`StatusCode(int)` returns `StatusCodeHttpResult`, whose status is only known at run time, so the type cannot describe it statically and does not implement `IEndpointMetadataProvider`. `Conflict()` returns the `Conflict` type, whose `PopulateMetadata` adds a 409 response entry. Prefer the named factory whenever the status is fixed.
- Results.Ok and TypedResults.Ok produce different status codes or bodies at run time.
- Calling TypedResults inside a handler declared as IResult still documents the response.
- ASP.NET Core inspects the returned object at run time to build OpenAPI metadata.
- TypedResults.StatusCode(409) documents a 409 response just like TypedResults.Conflict().
- TypedResults.InternalServerError has existed since Minimal APIs first shipped.
Assumes ASP.NET Core 10: typed results add cookie-redirect-disabling metadata only since 10, and TypedResults.InternalServerError exists only since 9.