Html.ActionLink 包括额外参数

发布于 2024-12-19 10:28:14 字数 2559 浏览 1 评论 0原文

使用以下代码:

@Html.ActionLink("News", "Index", new { controller = "News", area = "" }, new { @class = "News" })

我收到错误,因此当我阅读特定新闻文章时,链接生成为:

http://mywebsite/News/2011/12/2/my_slug

其中参数与我当前所在的页面匹配。这种情况也会发生在匹配相同 url 模式的其他控制器上(假设我有另一个名为“Presentations”的控制器,它完全匹配 News 的 url 模式)。从演示文稿页面,到新闻的链接将包括演示文稿参数,这些参数对于新闻控制器来说不是有效的输入。有道理吗?

我的路线定义为:

        routes.MapRoute(
            "News-Archive", // Route name
            "News/Archive", // URL with parameters
            new { controller = "News", action = "Archive" }, // Parameter defaults
            new[] { "mywebsite.Controllers" }
        );

        routes.MapRoute(
            "News-Stub", // Route name
            "News/{year}/{month}/{date}/{slug}", // URL with parameters
            new { controller = "News", action = "Index" }, // Parameter defaults
            new[] { "mywebsite.Controllers" }
        );

        routes.MapRoute(
            "News-ByDay", // Route name
            "News/{year}/{month}/{day}", // URL with parameters
            new { controller = "News", action = "Archive" }, // Parameter defaults
            new[] { "mywebsite.Controllers" }
        );

        routes.MapRoute(
          "News-ByMonth", // Route name
          "News/{year}/{month}", // URL with parameters
          new { controller = "News", action = "Archive" }, // Parameter defaults
          new[] { "mywebsite.Controllers" }
        );

        routes.MapRoute(
          "News-ByYear", // Route name
          "News/{year}", // URL with parameters
          new { controller = "News", action = "Archive" }, // Parameter defaults
          new[] { "mywebsite.Controllers" }
        );

        routes.MapRoute(
          "News-Default", // Route name
          "News", // URL with parameters
          new { controller = "News", action = "Index" }, // Parameter defaults
          new[] { "mywebsite.Controllers" }
        );

有人可以解释一下问题是否出在我使用 Html.ActionLink 或我的路线定义上吗?

编辑:

将 Actionlink 更改为:

@Html.ActionLink("News", "Index", new { controller = "News", area = "", year = string.Empty, month = string.Empty, day = string.Empty, slug = string.Empty}, null)

从演示文稿页面调用时会导致此结果(这些值用于演示文稿,而不是有效的新闻项目):

http://mywebsite/News?year=2011&month=12&slug=seo_overview

不确定为什么会导致重写发生变化。

Using the following code:

@Html.ActionLink("News", "Index", new { controller = "News", area = "" }, new { @class = "News" })

I'm getting an error so that when I'm on a particular news article, the link is generating as:

http://mywebsite/News/2011/12/2/my_slug

where the parameters match the page that I'm currently on. The also happens on other controllers matching the same url schema (imagine I have another controller called Presentations that matches the url scheme of News entirely). From the presentations pages, a link to news would include the presentations parameters which aren't valid input for the news controller. Make sense?

I have my routes defined as:

        routes.MapRoute(
            "News-Archive", // Route name
            "News/Archive", // URL with parameters
            new { controller = "News", action = "Archive" }, // Parameter defaults
            new[] { "mywebsite.Controllers" }
        );

        routes.MapRoute(
            "News-Stub", // Route name
            "News/{year}/{month}/{date}/{slug}", // URL with parameters
            new { controller = "News", action = "Index" }, // Parameter defaults
            new[] { "mywebsite.Controllers" }
        );

        routes.MapRoute(
            "News-ByDay", // Route name
            "News/{year}/{month}/{day}", // URL with parameters
            new { controller = "News", action = "Archive" }, // Parameter defaults
            new[] { "mywebsite.Controllers" }
        );

        routes.MapRoute(
          "News-ByMonth", // Route name
          "News/{year}/{month}", // URL with parameters
          new { controller = "News", action = "Archive" }, // Parameter defaults
          new[] { "mywebsite.Controllers" }
        );

        routes.MapRoute(
          "News-ByYear", // Route name
          "News/{year}", // URL with parameters
          new { controller = "News", action = "Archive" }, // Parameter defaults
          new[] { "mywebsite.Controllers" }
        );

        routes.MapRoute(
          "News-Default", // Route name
          "News", // URL with parameters
          new { controller = "News", action = "Index" }, // Parameter defaults
          new[] { "mywebsite.Controllers" }
        );

Can someone please explain if the problem lies in my use of Html.ActionLink or my route definitions?

EDIT:

Changing the Actionlink to:

@Html.ActionLink("News", "Index", new { controller = "News", area = "", year = string.Empty, month = string.Empty, day = string.Empty, slug = string.Empty}, null)

results in this when called from a presentation page (those values are for the presentation, not a valid news item):

http://mywebsite/News?year=2011&month=12&slug=seo_overview

Not sure why that's causing the rewriting to change.

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

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

发布评论

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

评论(1

唱一曲作罢 2024-12-26 10:28:14

由于没有提供 slug,操作链接假定当前的 slug:

使用

 ActionLink("News", "Index", new { controller = "News", area = "", slug="newslug"}, new { @class = "News" })

 ActionLink("News", "Index", new { controller = "News", area = "", slug = null}, new { @class = "News" })

设置 slug = null 也有可能重新使用当前的 slug,在这种情况下设置 slug = 空字符串。

The action link assumes the current slug since no slug was provided:

Use

 ActionLink("News", "Index", new { controller = "News", area = "", slug="newslug"}, new { @class = "News" })

or

 ActionLink("News", "Index", new { controller = "News", area = "", slug = null}, new { @class = "News" })

There is a chance that setting the slug = null would also re-use the current slug, in that case set the slug = Empty String.

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