【问题标题】:Stuck in a redirect loop陷入重定向循环
【发布时间】:2015-12-06 00:40:23
【问题描述】:

如果用户的状态为 0,我想要重定向到页面的基本控制器中有一段代码。

if (UserService.IsLoggedIn())
{
    model.IsLoggedIn = true;
    model.CurrentUser = UserService.GetCurrentUser();
    model.UserProfile = svc.GetByUserId(model.CurrentUser.Id);
    if (model.UserProfile.OnboardStatus == 0)
    {
        HttpContext.Response.Redirect("/user/onboard/"+ model.UserProfile.Id);
    }
}

我陷入了重定向循环,不确定如何解决。

【问题讨论】:

  • 这个控制器代码与什么路由相关联?
  • UserController 是否基于基本控制器?

标签: c# redirect


【解决方案1】:

这是因为您也从 BaseController 继承了 UserController

解决方案 1:不要从 BaseController 继承 UserController

解决方案 2

我认为您在OnActionExecuting 中的BaseController 中有这部分代码。

如果是这样,您可以按如下方式更新您的代码。

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
        if (UserService.IsLoggedIn())
        {
            model.IsLoggedIn = true;
            model.CurrentUser = UserService.GetCurrentUser();
            model.UserProfile = svc.GetByUserId(model.CurrentUser.Id);
            var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower();
            var actionName = filterContext.ActionDescriptor.ActionName.ToLower();
            if (controllerName != "user" && actionName != "onboard" && model.UserProfile.OnboardStatus == 0)
            {
                HttpContext.Response.Redirect("/user/onboard/"+ model.UserProfile.Id);
                }
            }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 2010-11-09
    • 1970-01-01
    • 2012-04-30
    • 2021-07-06
    • 2011-11-11
    相关资源
    最近更新 更多