Skip to content
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

Add example to route all unhandled requests #92

Merged
merged 2 commits into from
Jun 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ services
});
```

#### Route All Unhandled Requests

You can also route all unhandled requests to another server, e.g.

```csharp
app.UseStatusCodePages(async statusCodeContext =>
{
var context = statusCodeContext.HttpContext;
if (context.Response.StatusCode == StatusCodes.Status404NotFound)
{
var request = context.Features.Get<IHttpRequestFeature>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just use context.Request?

Copy link
Contributor Author

@dahlbyk dahlbyk Jun 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to stitch back together approximately what the request path+query was from HttpRequest, but passing through RawTarget unaltered seems more correct from a proxy standpoint (e.g. it works for an OPTIONS * request)

Gets or sets the request target as it was sent in the HTTP request.

if (request != null)
{
await context.HttpProxyAsync($"https://example.com{request.RawTarget}");
}
}
});
```

#### Existing Controller

You can define a proxy over a specific endpoint on an existing `Controller` by leveraging the `ProxyAsync` extension methods.
Expand Down