C# 陷入重定向循环

C# 陷入重定向循环,c#,redirect,C#,Redirect,如果用户的状态为0,我希望在基本控制器中重定向到页面的代码块 if (UserService.IsLoggedIn()) { model.IsLoggedIn = true; model.CurrentUser = UserService.GetCurrentUser(); model.UserProfile = svc.GetByUserId(model.CurrentUser.Id); if (model.UserProfile.OnboardStatus =

如果用户的状态为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);
    }
}

我陷入了重定向循环,我不确定如何修复它。

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

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

解决方案2

我假设您在
onaction中有这部分代码,在
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);
                }
            }
}

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

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

解决方案2

我假设您在
onaction中有这部分代码,在
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);
                }
            }
}

此控制器代码与哪个路由关联?UserController是否基于基本控制器?此控制器代码与哪个路由关联?UserController是否基于基本控制器?