ASP.NET 4.0 webforms routing within master page
I just want to asked if there's a workaround in url routing using masterpage. I put the links in my masterpage (i.e. first link is a href="Bookstore/CSS" and the other link is a href="tools/toolx") if i click the first link it redirect me to the correct url which is localhost:2039/Bookstore/but if i click the next link it will redirect me to localhost:2039/tools/Bookstore/CSS but the link should be localhost:2039/Bookstore/CSS.
Here is the code
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");
}
masterpage source code
<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">
&开发者_开发知识库lt;/div>
In the Menu Item click event try to get the item which is clicked and then use response.RedirectToRoute method. It requests to a new URL by using route name which you have specified in global.asax page . The sample code is :-
protected void NavigationMenu_MenuItemClick(object sender, MenuEventArgs e)
{
MenuItem item = e.Item;
if (item.Text == "Categories")
{
Response.RedirectToRoute("View Category");
}
}
The url routing for "View Category" mentioned in global page is
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);
}
精彩评论