Local主机在重定向到特定URL的同时,将您重定向多次

发布于 2025-02-06 18:57:21 字数 1236 浏览 1 评论 0原文

我创建了一个操作过滤器来检查一些语句,然后将客户端重定向到特定的操作或路由,但是当我运行应用程序浏览器时,请显示此页面: page image

我的代码:

public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
{
    var customer = await _workContext.GetCurrentCustomerAsync();

    var vendor = _vendorRepository.GetAll()
        .FirstOrDefault(w => w.CustomerId == customer.VendorId);
    if (vendor == null)
        return;                

    if (!(vendor.ActivationDate < DateTime.Now))
        return;
    RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
    redirectTargetDictionary.Add("action", "CheckVendorDate");
    redirectTargetDictionary.Add("controller", "VendorActivation");

    context.Result=new RedirectToRouteResult(redirectTargetDictionary);
}

update update

如果您想检查操作和Controler名称在OnaThorizationAsync方法中,以防止引起递归重定向或等等,请使用此代码:

 var descriptor = context?.ActionDescriptor as ControllerActionDescriptor;

           var action = descriptor.ActionName;
            var controllerName = descriptor.ControllerName;

I created an action filter to check some statements then redirect client to specific action or route, but when I run the application browser shows this page:
Page Image

My code:

public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
{
    var customer = await _workContext.GetCurrentCustomerAsync();

    var vendor = _vendorRepository.GetAll()
        .FirstOrDefault(w => w.CustomerId == customer.VendorId);
    if (vendor == null)
        return;                

    if (!(vendor.ActivationDate < DateTime.Now))
        return;
    RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
    redirectTargetDictionary.Add("action", "CheckVendorDate");
    redirectTargetDictionary.Add("controller", "VendorActivation");

    context.Result=new RedirectToRouteResult(redirectTargetDictionary);
}

Update :

If you want to check action and controller name in OnAuthorizationAsync method , to prevent causing recursive redirection or etc , use this code :

 var descriptor = context?.ActionDescriptor as ControllerActionDescriptor;

           var action = descriptor.ActionName;
            var controllerName = descriptor.ControllerName;

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

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

发布评论

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

评论(1

王权女流氓 2025-02-13 18:57:21

您似乎是在引起递归重定向,需要进行额外检查以防止重定向/vendoractivation/checkvendordate如果上下文来自该路线。

if (context.ActionDescriptor.ActionName == "CheckVendorDate" &&
        context.ActionDescriptor.ControllerDescriptor.ControllerName == "VendorActivation")
    return;

You seem to be causing recursive redirection, an additional check is needed to prevent redirection to /VendorActivation/CheckVendorDate if context is from that route.

if (context.ActionDescriptor.ActionName == "CheckVendorDate" &&
        context.ActionDescriptor.ControllerDescriptor.ControllerName == "VendorActivation")
    return;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文