ASP.NET路由不起作用(仅加载一个路由)

本文关键字:路由 一个 NET 不起作用 ASP 加载 | 更新日期: 2023-09-27 18:29:54

当我在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", "" }});

主页.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);
    }

当我点击Matches链接

ASP.NET路由不起作用(仅加载一个路由)

时,它只加载ACcountDetails.aspx,而不是Matches

您的Route.config运行良好。您添加的路线不正确。您的路由"Account/Profile/{username}/{accountDetails}""Account/Profile/{username}/{matches}"具有相同的格式,这就是为什么两个路由中只有一个可用。例如,Account/Profile/TestUser/me具有与两个路由相同的格式。更改一个路由图(例如Account/{username}/{matches}