【问题标题】:HTML.ActionLink() generating incorrect URLsHTML.ActionLink() 生成不正确的 URL
【发布时间】:2011-11-14 19:52:29
【问题描述】:

我很难让 HTML.ActionLink() 创建正确的链接。

当我在 http://localhost:49242/

我的选择是:

但是当我导航到http://localhost:49242/en-us/PinnedSites 时,我的选择是:

当然,所有这些网址都失败了。

这是我的html:

.cshtml (Html / razor)

<li id="nav_pinning">
    @Html.ActionLink( "pinned sites", "index", "PinnedSites")</li>
<li id="nav_addons">
    @Html.ActionLink("add-ons", "index", "addons")</li>
<li id="nav_control">
    @Html.ActionLink("tracking protection lists", 
                    "index", 
                    "trackingprotectionlists",
                    null,
                    null)</li>

Global.asax.cs

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("favicon.ico");
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "Default", // Route name
            "{culture}/{controller}/{action}", 
            new { culture = "en-us", 
                controller = "Home", 
                action = "Index", 
            }
        );
    }

我的问题在哪里? /PinnedSites/ 是否有可能实际上并没有转到 PinnedSitesController?

(我会积极关注这个,如果你想让我尝试一下或者提供更多代码,请发表评论。)

【问题讨论】:

    标签: asp.net asp.net-mvc razor url-routing


    【解决方案1】:

    您的项目中有 HomeController 吗?如果是这样,您应该按照以下方式注册默认控制器:

     routes.MapRoute(
             "Default", // Route name
             "{controller}/{action}/{id}", // URL with parameters
             new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    

    但是,除此之外,您是否尝试过简单地将 Route 直接连接到您的 PinnedSites 控制器?

    public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("favicon.ico");
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{culture}/{controller}/{action}", 
                new { culture = "en-us", 
                    controller = "PinnedSites", 
                    action = "Index", 
                }
            );
        }
    

    【讨论】:

    • 不完全是答案,但让我朝着正确的方向思考以找出答案。谢谢!
    • 嗨@Civilian 我有同样的问题。你的解决方案是什么?欢呼
    • 嘿@Franva 抱歉,这是 3 年前的事了,所以我不记得了,也没有代码了。但我认为这是我注册路线的方式有误,Razor 代码是正确的。
    • 嗨@Civilian不用担心,我已经想通了。我使用了错误的参数:)
    猜你喜欢
    • 2017-09-09
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多