ASP.net WebForms - Using GetRouteUrl in markup

天大地大妈咪最大 提交于 2019-12-10 10:35:43

问题


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>

回答1:


There is a special syntax for using routes in markup: http://msdn.microsoft.com/en-us/library/dd329551.aspx#Y800

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



回答2:


right syntax

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


来源:https://stackoverflow.com/questions/5326935/asp-net-webforms-using-getrouteurl-in-markup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!