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

W jaki sposób przekierować przychodzące żądanie? np. / pod /hangfire #3

Closed
sulmar opened this issue Dec 2, 2021 · 1 comment
Closed
Assignees

Comments

@sulmar
Copy link
Owner

sulmar commented Dec 2, 2021

No description provided.

@sulmar sulmar self-assigned this Dec 2, 2021
@sulmar
Copy link
Owner Author

sulmar commented Dec 2, 2021

Wystarczy podpiąć mapowanie z wywołać przekierowanie:

 app.UseEndpoints(endpoints =>
{
  endpoints.MapGet("/", async context => context.Response.Redirect("hangfire"));
}

Dla większej czytelności i wygody można utworzyć własną metodę rozszerzającą:

public static class EndpointExtensions
    {
        public static IEndpointRouteBuilder Redirect(
            this IEndpointRouteBuilder endpoints,
            string from, string to)
        {
            return Redirect(endpoints,
                new Redirective(from, to));
        }

        public static IEndpointRouteBuilder RedirectPermanent(
            this IEndpointRouteBuilder endpoints,
            string from, string to)
        {
            return Redirect(endpoints,
                new Redirective(from, to, true));
        }

        public static IEndpointRouteBuilder Redirect(
            this IEndpointRouteBuilder endpoints,
            params Redirective[] paths
        )
        {
            foreach (var (from, to, permanent) in paths)
            {
                endpoints.MapGet(from, async http => { http.Response.Redirect(to, permanent); });
            }

            return endpoints;
        }
    }

    public record Redirective(string From, string To, bool Permanent = false);

na podst. https://khalidabuhakmeh.com/simple-redirects-with-aspnet-core-endpoint-routing

@sulmar sulmar closed this as completed Dec 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant