gpt4 book ai didi

asp.net-mvc - 通过操作过滤器注入(inject)引荐来源网址操作?

转载 作者:行者123 更新时间:2023-12-02 20:33:31 24 4
gpt4 key购买 nike

有没有办法从操作过滤器注入(inject)引用者操作?假设我有一个来自操作 X 的 View 。在模具 View 中,我调用操作 Y,并且我想再次重定向到操作 X。(有多个 X 操作调用操作 Y)。我认为如果我有一个参数调用referrerAction 和一个用前一个操作填充它的操作过滤器,那就太好了。可能吗?

谢谢。

最佳答案

这是我的做法:

  public class ReturnPointAttribute : Attribute
{
}

public class BaseController: Controller
{
private string returnPointUrl = null;
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
if (filterContext.ActionDescriptor.IsDefined(typeof(ReturnPointAttribute), true))
returnPointUrl = filterContext.HttpContext.Request.Url.ToString();
}
public ActionResult RedirectOrReturn<T>(Expression<Action<T>> action) where T : BaseController
{
return returnPointUrl.IsNullOrEmpty()
? MyControllerExtensions.RedirectToAction(this, action)
: (ActionResult)Redirect(returnPointUrl);
}
}

现在,您可以使用 [ReturnPoint] 标记 X 个操作,如果您想返回,请调用 RedirectOrReturn()。

我不使用 UrlReferrer,因为它可能是错误的,而且我无法控制它的值。通过 ReturnPoint,您还可以拥有组,例如[ReturnPoint("订单")] 和 RedirectOrReturn("订单")。

当然,您可以在 OnActionExecuted 中拥有更多自动行为 - 例如它可以检查返回结果是否为Redirect,如果有值则自动转到ReturnPoint。或者您可以使用 [ReturnPoint(Automatic=true)] 等来控制它。

关于asp.net-mvc - 通过操作过滤器注入(inject)引荐来源网址操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1565422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com