如何在 ASP.NET MVC 中找出 Action 从哪里路由到的?

发布于 2024-08-21 21:22:56 字数 299 浏览 8 评论 0原文

我认为标题没有任何意义,所以我希望我能很好地解释它。

我有带有操作 EditItem 的控制器项目。 EditItem 从许多地方路由到,例如

/Item/Browse/ByCategory
/Item/Browse/ByType
/Item/Browse/ByWhatever

我真正想要的是将用户返回到他在项目上单击“编辑”的页面。

我知道我可以通过为 EditItem 操作传递 ?ReturnUrl 参数来做到这一点,但我一直想知道,是否可以找出用户来自哪里,例如引用者...

I don't think title is making any sense, so I hope that I can explain it well enough.

I have Controler Item with Action EditItem. EditItem is routed to from many places, such as

/Item/Browse/ByCategory
/Item/Browse/ByType
/Item/Browse/ByWhatever

What I'd really like is to return the user to page from where he clicked Edit on an item.

I know I can do this by passing an ?ReturnUrl parameter for EditItem action, but I keep wondering, is it possible to find out from where did user came from, something like referer...

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

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

发布评论

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

评论(3

蓝梦月影 2024-08-28 21:22:56

刚刚创建了测试解决方案,所以我确信这会起作用。
1) 在默认路由之前创建新路由:

routes.MapRoute("EditItem",
   "EditItem/{referrer}/{id}",
   new {controller = "Item", action = "EditItem",id = "",referrer = "ByCategory"}
);

2) 在 3 个视图中的任意一个上使用此链接到 EditItem:

<%= Html.RouteLink("Edit Item 1", "EditItem", 
    new {referrer = ViewContext.RouteData.Values["action"], id = 1}) %>

3) 在 EditItem 视图上使用此后退按钮:

<%= Html.RouteLink("Back", "Default", 
    new { action = ViewContext.RouteData.Values["referrer"]})%>

使用路由使 URL 更加美观且用户友好。

Just created test solution, so i'm sure this would work.
1) Create new route before default one:

routes.MapRoute("EditItem",
   "EditItem/{referrer}/{id}",
   new {controller = "Item", action = "EditItem",id = "",referrer = "ByCategory"}
);

2) Use this link to EditItem on any of your 3 views:

<%= Html.RouteLink("Edit Item 1", "EditItem", 
    new {referrer = ViewContext.RouteData.Values["action"], id = 1}) %>

3) Use this Back button on the EditItem view:

<%= Html.RouteLink("Back", "Default", 
    new { action = ViewContext.RouteData.Values["referrer"]})%>

Working with Routes makes URLs more beautiful and user-friendly.

梦里梦着梦中梦 2024-08-28 21:22:56

除了传递 returnUrl 或检查 HttpContext.Current.Request.Referer 属性之外,没有其他方法。但是 Referer 为您提供了需要解析以提取 Action 的字符串值。

There is no other way except passing returnUrl or checking HttpContext.Current.Request.Referer property. But Referer gives you string value that needs parsing to exctract Action.

苏大泽ㄣ 2024-08-28 21:22:56

啊,当然一切皆有可能。在您的控制器(或者最好是基本控制器)中覆盖 OnActionExecuted 和 OnActionExecuting 并将您以前的 URL 放入会话中。在从具有此实现的控制器继承的任何控制器中使用您的会话变量(如果您不从自定义基础继承,则仅使用此控制器)。

 protected string NextBackUrl {get; set;}
 protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {

            HttpContext.Current.Session["PreviousURL"] = NextBackUrl;
            base.OnActionExecuted(filterContext);
        }

 protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

                if (Request.Url != null && Request.UrlReferrer != null && Request.Url != Request.UrlReferrer)
                {
                    NextBackUrl = Request.UrlReferrer.AbsoluteUri;
                }

            base.OnActionExecuting(filterContext);
        }

Ah, but of course everything is possible. In your controller (or preferably base controller) override OnActionExecuted and OnActionExecuting and place your previous URL into session. Use your session variable in any controller that inherits from the one with this implementation (or just this controller if you don't inherit from a custom base).

 protected string NextBackUrl {get; set;}
 protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {

            HttpContext.Current.Session["PreviousURL"] = NextBackUrl;
            base.OnActionExecuted(filterContext);
        }

 protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

                if (Request.Url != null && Request.UrlReferrer != null && Request.Url != Request.UrlReferrer)
                {
                    NextBackUrl = Request.UrlReferrer.AbsoluteUri;
                }

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