MVC - 在构造函数内重定向

发布于 2024-10-14 04:02:15 字数 453 浏览 1 评论 0原文

我想知道如果需要的话,如何在控制器构造函数内重定向请求?

例如: 在构造函数内部,我需要使用动态值初始化一个对象,在某些情况下我不想这样做,在这种情况下我想重定向到其他地方。 同样,构造函数的其余部分也不会执行“原始后续操作”。

我该怎么做呢? 谢谢

编辑#1

最初我使用:

  public override void OnActionExecuting(ActionExecutingContext filterContext) 

在那里我可以重定向到其他一些控制器/操作/url,但后来,我需要更改我的控制器,我在他的构造函数中初始化一个变量并有一些确实需要重定向请求的代码:P

我也需要这个,因为 OnActionExecuting 在控制器构造函数之后执行。 按照我的逻辑,重定向需要在那里完成。

I would like to know how am I able to redirect the request inside the controller constructor if I need to do it?.

For example:
Inside the constructor I need to initialize an object with an dynamic value, in some cases I don't want to do it and in that case I want to redirect to some other place.
At the same way the rest of the constructor will not be executed neither the "original following action".

How can I do it?
Thank you

EDIT #1

Initially I used:

  public override void OnActionExecuting(ActionExecutingContext filterContext) 

There I could redirect to some other controller/action/url, but later in time, I needed to change my controller, where I initialize an variable in his constructor and have some code that really needs to redirect the request :P

I need this also because the OnActionExecuting executes AFTER the controller constructor.
And in my logic, the redirect needs to be done there.

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

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

发布评论

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

评论(1

灯角 2024-10-21 04:02:15

在控制器构造函数内执行重定向不是一个好习惯,因为上下文可能未初始化。标准做法是编写自定义操作属性并覆盖 OnActionExecuting 方法并在里面执行重定向。示例:

public class RedirectingActionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if (someConditionIsMet)
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
            {
                controller = "someOther",
                action = "someAction"
            }));
        }
    }
}

然后用此属性装饰您想要重定向的控制器。要非常小心,不要用此属性装饰您要重定向到的控制器,否则您将陷入无限循环。

所以你可以:

[RedirectingAction]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        // This action is never going to execute if the 
        // redirecting condition is met
        return View();
    }
}

public class SomeOtherController : Controller
{
    public ActionResult SomeAction()
    {
        return View();
    }
}

Performing redirects inside the controller constructor is not a good practice because the context might not be initialized. The standard practice is to write a custom action attribute and override the OnActionExecuting method and perform the redirect inside. Example:

public class RedirectingActionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if (someConditionIsMet)
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
            {
                controller = "someOther",
                action = "someAction"
            }));
        }
    }
}

and then decorate the controller which you would like to redirect with this attribute. Be extremely careful not to decorate the controller you are redirecting to with this attribute or you are going to run into an endless loop.

So you could:

[RedirectingAction]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        // This action is never going to execute if the 
        // redirecting condition is met
        return View();
    }
}

public class SomeOtherController : Controller
{
    public ActionResult SomeAction()
    {
        return View();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文