aspnetcore/fundamentals/minimal-apis/includes/minimal-apis7.md
:::moniker range="= aspnetcore-7.0"
This document:
The Minimal APIs consist of:
[!INCLUDE WebApplication]
The following table lists some of the middleware frequently used with Minimal APIs.
The following sections cover request handling: routing, parameter binding, and responses.
A configured WebApplication supports Map{Verb} and xref:Microsoft.AspNetCore.Builder.EndpointRouteBuilderExtensions.MapMethods%2A where {Verb} is a camel-cased HTTP method like Get, Post, Put or Delete:
The xref:System.Delegate arguments passed to these methods are called "route handlers".
[!INCLUDE route handling]
Route handlers support the following types of return values:
IResult based - This includes Task<IResult> and ValueTask<IResult>string - This includes Task<string> and ValueTask<string>T (Any other type) - This includes Task<T> and ValueTask<T>| Return value | Behavior | Content-Type |
|---|---|---|
IResult | The framework calls IResult.ExecuteAsync | Decided by the IResult implementation |
string | The framework writes the string directly to the response | text/plain |
T (Any other type) | The framework JSON-serializes the response | application/json |
For a more in-depth guide to route handler return values see xref:fundamentals/minimal-apis/responses
app.MapGet("/hello", () => "Hello World");
app.MapGet("/hello", () => new { Message = "Hello World" });
The following code returns a xref:Microsoft.AspNetCore.Http.TypedResults:
app.MapGet("/hello", () => TypedResults.Ok(new Message() { Text = "Hello World!" }));
Returning TypedResults is preferred to returning xref:Microsoft.AspNetCore.Http.Results. For more information, see TypedResults vs Results.
app.MapGet("/hello", () => Results.Ok(new { Message = "Hello World" }));
The following example uses the built-in result types to customize the response:
app.MapGet("/hello", () => Results.Json(new { Message = "Hello World" }));
app.MapGet("/405", () => Results.StatusCode(405));
app.MapGet("/text", () => Results.Text("This is some text"));
<a name="stream7"></a>
var proxyClient = new HttpClient();
app.MapGet("/pokemon", async () =>
{
var stream = await proxyClient.GetStreamAsync("http://contoso/pokedex.json");
// Proxy the response as JSON
return Results.Stream(stream, "application/json");
});
See xref:fundamentals/minimal-apis/responses#stream7 for more examples.
app.MapGet("/old-path", () => Results.Redirect("/new-path"));
app.MapGet("/download", () => Results.File("myfile.text"));
<a name="binr7"></a>
[!INCLUDE results-helpers]
Applications can control responses by implementing a custom xref:Microsoft.AspNetCore.Http.IResult type. The following code is an example of an HTML result type:
We recommend adding an extension method to xref:Microsoft.AspNetCore.Http.IResultExtensions?displayProperty=fullName to make these custom results more discoverable.
The xref:Microsoft.AspNetCore.Http.IResult interface can represent values returned from Minimal APIs that don't utilize the implicit support for JSON serializing the returned object to the HTTP response. The static Results class is used to create varying IResult objects that represent different types of responses. For example, setting the response status code or redirecting to another URL.
The types implementing IResult are public, allowing for type assertions when testing. For example:
You can look at the return types of the corresponding methods on the static TypedResults class to find the correct public IResult type to cast to.
See xref:fundamentals/minimal-apis/responses for more examples.
See xref:fundamentals/minimal-apis/min-api-filters
Routes can be protected using authorization policies. These can be declared via the [Authorize] attribute or by using the xref:Microsoft.AspNetCore.Builder.AuthorizationEndpointConventionBuilderExtensions.RequireAuthorization%2A method:
The preceding code can be written with xref:Microsoft.AspNetCore.Builder.AuthorizationEndpointConventionBuilderExtensions.RequireAuthorization%2A:
The following sample uses policy-based authorization:
The [AllowAnonymous]
allows unauthenticated users to access endpoints:
Routes can be CORS enabled using CORS policies. CORS can be declared via the [EnableCors] attribute or by using the
xref:Microsoft.AspNetCore.Builder.CorsEndpointConventionBuilderExtensions.RequireCors%2A method. The following samples enable CORS:
For more information, see xref:security/cors?view=aspnetcore-6.0
<a name="openapi7"></a>
<!-- # Differences between Minimal APIs and APIs with controllers Moved to uid: tutorials/min-web-api -->:::moniker-end