MVC Actionlink或可能的路由问题

时间:2012-02-21 14:28:27

标签: asp.net-mvc-3 url-routing actionlink

我在应用程序中遇到了很多挑战,并且在这里遇到了很多挫折和文章,我以为我已经把它钉了但是现在已经测试了我的应用程序了,我有一个最后的故障。

我将默认值移至底部,购买路线移至顶部,然后我发现由于参数不同,我需要为每个页面创建一条路线,否则会损坏。

我遇到的最后一个问题是,一旦你点击从布局页面继承的菜单栏项目(第一页上的任何点击都能正常工作),你在后续页面上的Home上会得到错误的结果

第一页给出了如下链接:

http://localhost:34396/Products/2/1

后续页面给出:

除了Home之外,菜单栏上的每个链接都

http://localhost:34396/Products/2/1

任何想法真的很受欢迎,它让我疯狂,我做的任何事情都会让它更加突破,不确定它是否因为我正在做链接的方式或我的路由中的一个有趣的错误。

干杯, 史蒂夫。

<div id="menu-wrapper">
    <div id="menu" class="container">
        <ul>
            <li>@Html.ActionLink("Home", "Index", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
            <li>@Html.ActionLink("Products", "Products", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
            <li>@Html.ActionLink("Existing Customers", "Existing", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
            <li>@Html.ActionLink("Claims", "Claims", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
            <li>@Html.ActionLink("FAQ", "FAQ", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
            <li>@Html.ActionLink("Contact Us", "ContactUs", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
        </ul>
    </div>


routes.MapRoute(
"Purchase", // Route name
"Purchase/{AgentId}/{ProductId}/{SchemeId}/{CoverTypeId}/{CoverLevelId/{SubAgentId}",     // URL with parameters
new { controller = "Home", action = "Purchase", AgentId = UrlParameter.Optional, ProductId = UrlParameter.Optional, SchemeId = UrlParameter.Optional, CoverTypeId = UrlParameter.Optional, CoverLevel = UrlParameter.Optional, SubAgent = UrlParameter.Optional } ,  // Parameter defaults
new[] { "BrochureWare.Controllers" }
);

routes.MapRoute(
"Products", // Route name
"Products/{AgentId}/{SubAgentId}", // URL with parameters
new { controller = "Home", action = "Products", AgentId = UrlParameter.Optional,
subAgent = 0 },  // Parameter defaults
new[] { "BrochureWare.Controllers" }
);

        routes.MapRoute(
"MoreInfo_Annual", // Route name
"MoreInfo_Annual/{AgentId}/{SubAgentId}", // URL with parameters
new { controller = "Home", action = "MoreInfo_Annual", AgentId = 
UrlParameter.Optional, SubAgent = 0 },  // Parameter defaults
new[] { "BrochureWare.Controllers" }
);

            routes.MapRoute(
"FAQ", // Route name
"FAQ/{AgentId}/{SubAgentId}", // URL with parameters
new { controller = "Home", action = "FAQ", AgentId = UrlParameter.Optional, SubAgent = 
0 },  // Parameter defaults
new[] { "BrochureWare.Controllers" }
);

            routes.MapRoute(
"Existing", // Route name
"Existing/{AgentId}/{SubAgentId}", // URL with parameters
new { controller = "Home", action = "Existing", AgentId = UrlParameter.Optional,   
SubAgent = 0 },  // Parameter defaults
new[] { "BrochureWare.Controllers" }
);

            routes.MapRoute(
"ContactUs", // Route name
"ContactUs/{AgentId}/{SubAgentId}", // URL with parameters
new { controller = "Home", action = "ContactUs", AgentId = UrlParameter.Optional,     
SubAgent = 0 },  // Parameter defaults
new[] { "BrochureWare.Controllers" }
);

            routes.MapRoute(
"Claims", // Route name
"Claims/{AgentId}/{SubAgentId}", // URL with parameters
new { controller = "Home", action = "Claims", AgentId = UrlParameter.Optional,   
SubAgent = 0 },  // Parameter defaults
new[] { "BrochureWare.Controllers" }
);

            routes.MapRoute(
"Default", // Route name
"{AgentShortCode}/{referid}", // URL with parameters
new { controller = "Home", action = "Index", AgentShortCode = "Steve", referid = 1  
},   
// Parameter defaults
new[] { "BrochureWare.Controllers" }
);

1 个答案:

答案 0 :(得分:0)

您可以使用@ Html.RouteLink,并指定要链接到的路由的名称,以强制ASP.NET选择您要使用的路由。

所以你可以这样做:

<div id="menu-wrapper">
    <div id="menu" class="container">
        <ul>
            <li>@Html.RouteLink("Home", "Default", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
            <li>@Html.RouteLink("Products", "Products", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
            <li>@Html.RouteLink("Existing Customers", "Existing", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
            <li>@Html.RouteLink("Claims", "Claims", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
            <li>@Html.RouteLink("FAQ", "FAQ", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
            <li>@Html.RouteLink("Contact Us", "ContactUs", new { AgentId = ViewData["AgentId"], SubAgentId = ViewData["ReferId"] })</li>
        </ul>
    </div>