首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包含额外参数的Html.ActionLink

包含额外参数的Html.ActionLink
EN

Stack Overflow用户
提问于 2011-12-06 03:15:13
回答 1查看 3.6K关注 0票数 0

使用以下代码:

代码语言:javascript
复制
@Html.ActionLink("News", "Index", new { controller = "News", area = "" }, new { @class = "News" })

我得到了一个错误,所以当我在一篇特定的新闻文章上时,链接生成为:

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

其中的参数与我当前所在的页面匹配。这也发生在匹配相同url模式的其他控制器上(假设我有另一个控制器,名为Presentation,它完全匹配News的url模式)。在演示页面中,指向新闻的链接将包含演示参数,这些参数对于新闻控制器来说不是有效的输入。讲得通?

我将我的路由定义为:

代码语言:javascript
复制
        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还是我的路由定义上吗?

编辑:

将操作链接更改为:

代码语言:javascript
复制
@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

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

EN

回答 1

Stack Overflow用户

发布于 2011-12-06 03:19:45

由于未提供任何插件,因此操作链接将采用当前的插件:

使用

代码语言:javascript
复制
 ActionLink("News", "Index", new { controller = "News", area = "", slug="newslug"}, new { @class = "News" })

代码语言:javascript
复制
 ActionLink("News", "Index", new { controller = "News", area = "", slug = null}, new { @class = "News" })

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8390570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档