1

我只是想问一下使用母版页的 url 路由是否有解决方法。我将链接放在我的母版页中(即第一个链接是 href="Bookstore/CSS",另一个链接是 href="tools/toolx"),如果我单击第一个链接,它会将我重定向到正确的 url,即 localhost :2039/Bookstore/但如果我点击下一个链接,它会将我重定向到 localhost:2039/tools/Bookstore/CSS,但链接应该是 localhost:2039/Bookstore/CSS。

这是代码

全球.asax

void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RouteTable.Routes.MapPageRoute("StoreRoute",
        "BookStore/{Name}",
        "~/Webpages/BookStore/ViewBookDemo.aspx");

        RouteTable.Routes.MapPageRoute("mytool",
        "tools/{Name}",
        "~/tools/tools.aspx");
    }

母版源代码

<div class="page">
    <div >
        <div class="title">
            <h1>
                URL Routing in MAsterpage
            </h1>
        </div>
        <div class="clear hideSkiplink">
            <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
                <Items>
                    <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
                </Items>
            </asp:Menu>
        </div>
    </div>
   <div class="clear"> </div>
   <div style=" width:90%">
  <div style="float:left; width:25%;border: 1px solid #009933;"> 
  <br />
    <a href="Bookstore/CSS">Click Here to go to bookstore</a>.
   <div class="clear"> </div>
   <p>
      <a href="tools/toolx">Click Here to go to tools</a>.
  </p> </div>
   <div style="float:right;width:73%">
   <div class="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
    </div>
   </div>
  </div>
  </div>
     <div class="clear"> </div>
<div class="footer">

</div>
4

1 回答 1

0

在菜单项单击事件中,尝试获取被单击的项目,然后使用 response.RedirectToRoute 方法。它使用您在 global.asax page 中指定的路由名称请求新的 URL。示例代码是: -

protected void NavigationMenu_MenuItemClick(object sender, MenuEventArgs e)

    {
        MenuItem item = e.Item;
        if (item.Text == "Categories")
        {
            Response.RedirectToRoute("View Category");
        }
    }

全局页面中提到的“查看类别”的 url 路由是

  public void RegisterRoutes(RouteCollection route)
    {
        route.MapPageRoute("View Category", "Categories/All", "~/Views/Categories.aspx");
    }


   void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RegisterRoutes(RouteTable.Routes);

    }
于 2012-05-03T02:44:16.280 回答