简体   繁体   中英

ASP.net WebForms - Using GetRouteUrl in markup

I have been trying to figure out how to use the Routing features with ASP.net 4.0 WebForms. I added a route to my route collection:

void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute(
        "about-route",
        "about/",
        "~/About.aspx"
    );
}

In my master page I tried to do the following:

<asp:HyperLink ID="asdf" runat="server" NavigateUrl='<%= GetRouteUrl("about-route", new {}) %>'>Test</asdf>

I got a compiler error: Server tags cannot contain <% ... %> constructs.

What is the proper way to create a route URL in a server control in Web Forms? I also need to include it in the following:

<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
    <Items>
        <asp:MenuItem NavigateUrl="ROUTE HERE" Text="Home"/>
        <asp:MenuItem NavigateUrl="ROUTE HERE" Text="About"/>
    </Items>
</asp:Menu>

在标记中使用路由有一种特殊的语法: http : //msdn.microsoft.com/zh-cn/library/dd329551.aspx#Y800

<asp:MenuItem NavigateUrl='<%$RouteUrl:about-route%>' Text="About"></asp:MenuItem>

正确的语法

<a href='<%$RouteUrl:routename=about-route %>' runat="server">Homepage</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM