Skip to content

Returning Responses #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Tracked by #17
LadyNaggaga opened this issue Aug 20, 2021 · 1 comment
Open
Tracked by #17

Returning Responses #20

LadyNaggaga opened this issue Aug 20, 2021 · 1 comment
Labels
Getting Started Docs designed to give developer quick overviews on how to implement specific features

Comments

@LadyNaggaga
Copy link
Contributor

No description provided.

@LadyNaggaga LadyNaggaga added the Getting Started Docs designed to give developer quick overviews on how to implement specific features label Aug 20, 2021
@LadyNaggaga LadyNaggaga changed the title Returning json object Returning Responses Aug 23, 2021
@captainsafia
Copy link
Collaborator

Returning responses from minimal APIs

Minimal APIs takes care of setting content types from common response formats. For example, an endpoint that returns a text payload will automatically encode the response with the text/plain content type.

app.MapGet("/", () => "Hello World!");
$ http http://localhost:5075
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Sun, 29 Aug 2021 01:57:46 GMT
Server: Kestrel
Transfer-Encoding: chunked

Hello World!

Objects returns from an endpoint are automatically encoced as a JSON response object.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => new ToDo(1, "Take out the trash!"));

app.Run();
record ToDo(int Id, string Task);
$ http http://localhost:5075
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Sun, 29 Aug 2021 02:03:23 GMT
Server: Kestrel
Transfer-Encoding: chunked

{
    "id": 1,
    "task": "Take out the trash!"
}

Returning Results from an endpoint

Minimal APIs also supports returning a standard IResult type from an endpoint. For example, to implement an endpoint that redirects to another endpoint.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/old-address", () => Results.Redirect("/new-address"));
app.MapGet("/new-address", () => "Welcome to the new space!");

app.Run();

Then when the user visits the "/old-address" they will receive a 302 redirect to the new location.

$ http http://localhost:5075/old-address
HTTP/1.1 302 Found
Content-Length: 0
Date: Sun, 29 Aug 2021 02:10:51 GMT
Location: /new-address
Server: Kestrel

A variety of extrension methods exist in the Http namespace for returning a wide variety of results, such as redirects, 404s, streams, and so forth.

We can also return streams and binary files from a minimal endpoint with these extension methods.

app.MapGet("/download/{name}", (string name) => {
    var file = GetFile(name);

    if (file is null)
    {
        return Results.NotFound();
    }

    return Results.File(file.Value);
});

Extending Results for library authors

As a library author, you might want to provide an API that allows users to return a custom Result from their endpoints. Minimal APIs provides support for a Results.Extensions static in the Microsoft.AspNetCore.Http namespace that can be used to expose extension methods that return a custom results type to the author. For example, suppose you wanted to provide an extension method that allowed developers to return a ToDoResult from their endpoints. In your library code, you can implement:

namespace MyLibraryNamespace;

public static class MyLibraryResults
{
  public static IResult MyLibraryResult(this IResultExtensions resultExtensions, string name)
    {
      return new MyLibraryType(name);
    }
  }
}

Then, users can invoke your custom results object in the endpoints within their application using the following:

using Microsoft.AspNetCore.Http;
app.MapGet("/view", () => Results.Extensions.MyLibraryResult("SomeText"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Getting Started Docs designed to give developer quick overviews on how to implement specific features
Projects
None yet
Development

No branches or pull requests

2 participants