创建 ActionLink,同时仍保留原始 Uri/路由值

发布于 2024-10-11 08:13:26 字数 572 浏览 3 评论 0原文

我有一个带有默认索引操作的控制器,以及许多其他操作,每个操作都呈现自己的视图。

当一个操作方法调用另一个操作时,浏览器中的 Uri 类似于

http://localhost/Home/Events /20100120/Sydney

例如,我如何在像这样的模板之一中创建操作链接,同时仍然保留其他路由值? http://localhost/Home/Events/20100120/Sydney/10PM

如果我这样做,我丢失了 Uri 中的所有其他路由值。

<%= Html.ActionLink("View 10PM times", "Events", "Home", new Dictionary<string, object>() { {"time", "10PM"} })%>

I have a controller with a default Index action, as well as many other actions each rendering their own view.

When an action method calls another action, the Uri in the browser is something like

http://localhost/Home/Events/20100120/Sydney

How would I create an Action Link in one of my templates like this for example, and still preserve the other route values?
http://localhost/Home/Events/20100120/Sydney/10PM

If I do this, I lose all the other route values in the Uri.

<%= Html.ActionLink("View 10PM times", "Events", "Home", new Dictionary<string, object>() { {"time", "10PM"} })%>

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

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

发布评论

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

评论(2

残龙傲雪 2024-10-18 08:13:26

路线数据会自动保存。假设您有以下路由:

routes.MapRoute(
    "MyRoute",
    "foo/{token1}/{token2}/{action}",
    new { controller = "Home", action = "Action1", token1 = "", token2 = "" }
);

现在您请求 /foo/t1/t2/Action1 并在视图中写入一个指向 Action2 的链接:

<%= Html.ActionLink("Action2", "Action2")%>

将生成以下 url :/foo/t1/t2/Action2。路由值被保留。

Route data is automatically preserved. Suppose that you have the following route:

routes.MapRoute(
    "MyRoute",
    "foo/{token1}/{token2}/{action}",
    new { controller = "Home", action = "Action1", token1 = "", token2 = "" }
);

Now you request /foo/t1/t2/Action1 and inside the view you write a link to Action2:

<%= Html.ActionLink("Action2", "Action2")%>

The following url will be generated: /foo/t1/t2/Action2. Route values are preserved.

泅人 2024-10-18 08:13:26

您可以像这样使用 Html.RouteLink() :

<%
    var routeData = new RouteValueDictionary(this.ViewContext.RouteData.Values);
    routeData.Add("time", "10PM");
%>
<%=Html.RouteLink("View 10PM times", routeData) %>

You can use the Html.RouteLink() like so:

<%
    var routeData = new RouteValueDictionary(this.ViewContext.RouteData.Values);
    routeData.Add("time", "10PM");
%>
<%=Html.RouteLink("View 10PM times", routeData) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文