ASP.NET路由无法正常工作(仅加载一条路由)

时间:2016-01-10 23:37:36

标签: c# asp.net asp.net-mvc

当我在Route.config中设置路由时,当我点击主页面上应该转到不同路由的按钮时,它只会转到route.config中的第一个路由。

Route.config:

        // Profile/username/AccountInfo Route
        routes.MapPageRoute("ProfileAccountDetails",
        "Account/Profile/{username}/{accountDetails}",
        "~/Account/Profile/AccountDetails.aspx", true,
        new RouteValueDictionary { 
            { "username", "" },
            { "accountDetails", "" }});

        // Profile/username/Matches Route
        routes.MapPageRoute("ProfileMatches",
        "Account/Profile/{username}/{matches}",
        "~/Account/Profile/Matches.aspx", true,
        new RouteValueDictionary { 
            { "username", "" },
            { "matches", "" }});

Master page.aspx:

<div id="navProfile">
        <ul id="navBarProfile">
            <li class="navItemProfile"><asp:LinkButton runat="server" ID="linkAccountInfo" CssClass="navLink" OnClick="AccountDetails_OnClick">Account Info</asp:LinkButton></li>
            <li class="navItemProfile"><asp:LinkButton runat="server" ID="linkMatches" CssClass="navLink" OnClick="Matches_OnClick">My Matches</asp:LinkButton></li>
        </ul>
    </div>

MasterPage.cs:

    protected void AccountDetails_OnClick(Object sender, EventArgs e)
    {
        RouteValueDictionary parameters = new RouteValueDictionary  
            { 
                {"username", currentUser.Username},
                {"accountDetails", "AccountDetails"}
            };
        VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "ProfileAccountDetails", parameters);
        Response.Redirect(vpd.VirtualPath);
    }
    protected void Matches_OnClick(Object sender, EventArgs e)
    {
        RouteValueDictionary parameters = new RouteValueDictionary  
            { 
                {"username", currentUser.Username},
                {"matches", "Matches"}
            };
        VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, "ProfileMatches", parameters);
        Response.Redirect(vpd.VirtualPath);
    }

当我点击匹配链接时,它只加载ACcountDetails.aspx而不是匹配

1 个答案:

答案 0 :(得分:2)

您的 Route.config 效果很好。您错误地添加了路线。您的路由"Account/Profile/{username}/{accountDetails}""Account/Profile/{username}/{matches}"具有相同的格式,这就是为什么只有两条路线中的一条有效的原因。例如,Account/Profile/TestUser/me具有与两个路由相同的格式。更改您的某个路由映射(例如Account/{username}/{matches}