从类中重定向到动作的正确方法? [英] Proper way to redirect to an action from within a class?

查看:98
本文介绍了从类中重定向到动作的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Web表单和Web表单之间拆分的项目. MVC(感谢您迈向MVC).

I have a project split between Web Forms & MVC (moving towards MVC, thankfully).

我有一个LoginManager类,该类接受一个IRedirectionManager类,并且根据用户属性(已验证,密码已过期,未接受条款),在重定向管理器上调用了不同的方法(RedirectToLogin,RedirectToAcceptTerms等) )

I have a LoginManager class that takes in an IRedirectionManager class, and based on user properties (is authenticated, password expired, hasn't accepted terms) calls different methods on the Redirection manager (RedirectToLogin, RedirectToAcceptTerms, etc.)

我正在为我们的MVC控制器构建和实施重定向管理器".

I'm working to build and implement the "Redirect Manager" for our MVC controllers.

  • 这应该是ActionFilter还是基类?看来我可以使用其中任何一种,但不确定是否确实如此.
  • 在MVC中重定向到动作或路由的适当方法是什么?对于Web表单RedirectManager,我传入了响应对象,并使用Response.Redirect转到页面.我很想能够使用RedirectToAction之类的东西来完成这项工作.
  • Should this be an ActionFilter or a Base Class? It appears that I could use either, but am not sure if that's actually the case.
  • What is the appropriate way to redirect to an action or route in MVC? For the web forms RedirectManager, I was passing in the response object and using Response.Redirect to get to the page. I'd love to be able to use something like RedirectToAction, etc. to make this work.

以帮助澄清. LoginManager:

public class LoginManager
{
    private IRedirectManager _redirectManager;
    private string _returnUrl;
    private ClaimsIdentity _currentUser;

    public LoginManager(string attemptingToAccessUrl, ClaimsIdentity identity, IRedirectManager redirectManager)
    {
        if (redirectManager == null) { throw new ArgumentNullException("redirectManager"); }
        if (identity == null) { throw new ArgumentNullException("identity"); }

        _redirectManager = redirectManager;
        _currentUser = identity;
        _returnUrl = attemptingToAccessUrl;
    }

    public void CheckLoginAndRedirect()
    {
        if (!_currentUser.IsAuthenticated)
        {
            if (string.IsNullOrWhiteSpace(_returnUrl)) { _redirectManager.RedirectToLogin(); }
            else { _redirectManager.RedirectToLogin(_returnUrl); }
        }

        if (_currentUser.IsDisabled()) { _redirectManager.RedirectToDisabled(); }

        if (UserRequiresPasswordReset()) { _redirectManager.RedirectToResetPassword(_returnUrl); }

        if (!_currentUser.HasAcceptedTerms()) {
            if (_currentUser.IsExternalUser()){ _redirectManager.RedirectToTermsAcceptance(_returnUrl); }
        }

        if (PasswordIsAboutToExpire())
        {
            if (!_currentUser.HasDeclinedToChangePassword()) { _redirectManager.RedirectToPasswordExpiringSoon(_returnUrl); }
        }
    }
}

这是WebFormsRedirectManager的示例:

public class WebFormsRedirectManager : IRedirectManager
{
    private HttpContext _httpContext;

    public WebFormsRedirectManager(HttpContext currentHttpContext)
    {
        if (currentHttpContext == null) { throw new ArgumentNullException("currentHttpContext");}

        _httpContext = currentHttpContext;
    }

    public void RedirectToLogin()
    {
        _httpContext.Response.Redirect("~/AccountManagement/Login");

    }
}

以及在其他Web表单页面继承的ProtectedPage类中如何使用它的示例:

And an example of how I use it in my ProtectedPage class, which other web forms pages inherit from:

public class ProtectedPage : BasePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        var redirector = new WebFormsRedirectManager(Context);
        new LoginManager(null, ClaimsPrincipal.Current.Identity as ClaimsIdentity, redirector).CheckLoginAndRedirect();
    }
}

推荐答案

如果您查看MVC源代码,则RedirectToAction()返回RedirectoToRouteResult对象.此类实际上是在做您正在做的事情-调用Response.Redirect().

If you have a look at the MVC source code RedirectToAction() returns a RedirectoToRouteResult object. This class is effectively doing what you are doing - calling Response.Redirect().

public override void ExecuteResult(ControllerContext context)
{
    ...

    string destinationUrl = UrlHelper.GenerateUrl(
        RouteName, null /* actionName */, null /* controllerName */, 
        RouteValues, Routes, context.RequestContext, 
        false /* includeImplicitMvcValues */);

    ...

    context.HttpContext.Response.Redirect(destinationUrl, endResponse: false);
}

这篇关于从类中重定向到动作的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆