是否可以在ASP.NET Core中为任何重定向创建拦截器?

发布于 2025-02-08 14:46:00 字数 114 浏览 1 评论 0原文

我正在创建一个用于检测重定向的工具,并将其显示在管理面板中。

我想以某种方式了解所有重定向,而没有开发人员参与其中。

在ASP.NET核心中是否可以?

如果是,我该怎么做?

I'm creating a tool to detect redirections and show them in an admin panel.

I want to somehow know about all redirections without the developer getting involved.

Is it possible in ASP.NET Core?

If yes, how can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-02-15 14:46:00

此问题必须需要开发人员干预,这可以存储在日志中。方便在管理面板中显示记录。

public class RedirectHandlerMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ILogger<RedirectHandlerMiddlerware> _logger;

    public RedirectHandlerMiddleware(RequestDelegate next, ILogger<RedirectHandlerMiddleware> logger)
    {
        _next = next;
        _logger = logger;
    }

    public async Task Invoke(HttpContext context)
    {
        await HandleRedirect(context, ex);
        await _next(context);
    }

    private Task HandleRedirect(HttpContext context)
    {
        if (context.Response.StatusCode == 302)
        {
            ...
            //context.Response.Redirect("...");
        }
        return Task.CompletedTask;
    }
}

您需要在startup.cs中注册。应该在app.usethentication();之后放置。

app.UseMiddleware<RedirectHandlingMiddleware>();

This problem must require developer intervention, which can be stored in logs. Convenient to display records in your admin panel.

public class RedirectHandlerMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ILogger<RedirectHandlerMiddlerware> _logger;

    public RedirectHandlerMiddleware(RequestDelegate next, ILogger<RedirectHandlerMiddleware> logger)
    {
        _next = next;
        _logger = logger;
    }

    public async Task Invoke(HttpContext context)
    {
        await HandleRedirect(context, ex);
        await _next(context);
    }

    private Task HandleRedirect(HttpContext context)
    {
        if (context.Response.StatusCode == 302)
        {
            ...
            //context.Response.Redirect("...");
        }
        return Task.CompletedTask;
    }
}

You need to register in Startup.cs. It should be placed after app.UseAuthentication();.

app.UseMiddleware<RedirectHandlingMiddleware>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文