2011-09-01 144 views
1

我只想問是否有使用masterpage的URL路由的解決方法。我把鏈接放在我的主頁面(即第一個鏈接是href =「Bookstore/CSS」,另一個鏈接是href =「tools/toolx」)如果我點擊第一個鏈接,它會將我重定向到正確的url, :2039/Bookstore /但是如果我點擊下一個鏈接,它會將我重定向到localhost:2039/tools/Bookstore/CSS,但鏈接應該是localhost:2039/Bookstore/CSS。ASP.NET 4.0 webforms在主頁面內路由

下面是代碼

Global.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> 

回答

0

在菜單項單擊事件設法得到它被點擊項目,然後用響應.RedirectToRoute方法。它通過使用您在global.asax頁面中指定的路由名稱來請求一個新的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); 

    }