1

I'm trying to find a way to generate routes for ASP.NET Web Forms in the declarative ASPX part.

This example works but it doesn't format the route properly in the form of /name/value/ but instead does /name/value?param2=0.

   <asp:HyperLink ID="EventLink" NavigateUrl="<%$RouteUrl:param=myparan, param2=0 %>" Text="More" runat="server" />

I created a second method as shown below, thinking I could pull out the routevalues

<a href="<%# GetMyRoute(Page) %>") %>">

GetMyRoute tries to pull out the route values but RouteData is always nulls:

page.RouteData.Values["MyParam"]

I have 2 specific questions.

  1. How do I pass the routed values into my GetMyRoute method? What is the syntax that allows me too do this GetMyRoute("<%$RouteValue:MyRoute%>).

  2. Is there some way for me to read the values using the RouteData object? Again, it returns null but the values are there in the Page_Load.

  3. How would I pass in a variable "<%$RouteUrl:param=myparan, param2=0 %>" for one of the params, say a property on the page?

** Partial Solution *

I found the following code. However, how does one pass in a page RouteValue as a param?

<asp:HyperLink ID="HyperLinkClient" runat="server"  
    NavigateUrl='<%# GetRouteUrl("ClientRoute", new {ClientID = Eval("ClientID")}) %>' > 
    Go to Client details 
</asp:HyperLink>

The following does NOT work because RouteData is null.

   <asp:HyperLink ID="HyperLinkClient" runat="server"  
        NavigateUrl='<%# GetRouteUrl("ClientRoute", new {ClientID = RouteData.Values["ClientId"] }) %>' > 
        Go to Client details 
    </asp:HyperLink>

Update The central "crux" of this question is how to pass in route parameters that were passed into the page without resulting to code-behind solution. Notice that RouteData.Values is null/empty when checked in the configuration shown above. I need to test the <%= syntax to see if that fixes it. The solutions proposed so far don't deal with the central issues.

Curtis White
  • 6,213
  • 12
  • 59
  • 83
  • Found this, http://stackoverflow.com/questions/2935367/how-to-create-routeurls-with-databound-parameters-declaratively – Curtis White Jun 06 '14 at 16:30

2 Answers2

0

When RouteConfig.cs includes the following route

public static void RegisterCustomRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Route1", "{name}/{value}", "~/default.aspx");
}

then you can refer to it using routename as

<%$RouteUrl:routename=Route1,name=MyParam,value=0%>

it will render

<a id="HyperLink1" href="/MyParam/0">...

Example: copy following code to default.aspx

<%@ Page Language="C#"  %>

Name=<%=RouteData.Values["name"] %>
Value=<%=RouteData.Values["value"] %>

<asp:HyperLink ID="HyperLink1" runat="server" 
NavigateUrl="<%$RouteUrl:routename=Route1,name=MyParam,value=0%>">
More</asp:HyperLink>

and call http://localhost/MyParam/0 - output will be

Name=MyParam 
Value=0

UPDATE:

To format an url from the code you can use

protected string GetMyRoute()
{
    return "/" + Page.RouteData.Values["name"] + "/" + Page.RouteData.Values["value"];
}

and

<a href="<%=GetMyRoute()%>">Link</a>

UPDATE 2:

For route that is defined as

routes.MapPageRoute("ClientRoute", "{ClientID}", "~/default.aspx");

you can call urls as http://localhost/something and use following aspx layout

<%@ Page Title="Home Page" Language="C#" Inherits="WebApplication1._Default"  %>
<a href="<%=GetRouteUrl("ClientRoute", new { ClientID = RouteData.Values["ClientID"] })%>">Link</a>

and code-behind

protected string GetRouteUrl(string routeName, RouteValueDictionary parameters)
{
    VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, routeName, parameters);
    return vpd == null ? "/" : vpd.ToString();
}
user2316116
  • 6,726
  • 1
  • 21
  • 35
0

Here is a clean solution of routing in ASP.NET web forms and I'm currently using this technique on my web forms' projects.

First: Using a static class to create all the urls for the entire application domain. (this is an example of this class with and without URL parameters)

public static class UrlMaker
{

    // example authentication urls
    public static string ToLogin() { return Path("~/login"); }
    public static string ToLogout() { return Path("~/logout"); }

    // example of a url with parameteres
    public static string ToCourse(int courseCode, string courseName) 
    { return Path("~/courses/{0}/{1}", courseCode, courseName); }

    //add more urls here ...


    //private url builder helper
    static string Path(string virtualPath){
        return VirtualPathUtility.ToAbsolute(virtualPath);
    }

    //overloaded private url builder helper
    static string Path(string virtualPath, params object[] args) {
        return Path(string.Format(virtualPath, args));
    }

}

Second: Your RouteConfig class in the App_Start folder will remain the same as before, in my case is like the code below:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes) 
    {
        routes.Ignore("{resource}.axd/{*pathInfo}");

        routes.MapPageRoute("Login", "login", "~/Login.aspx");
        routes.MapPageRoute("Logout", "logout", "~/Logout.aspx");
        routes.MapPageRoute("Course", "courses/{coursecode}/{coursename}",
                                          "~/Home/CoursePortal.aspx");
    }
}

Now anywhere in your application you could use the declared URLs above like this:

your .aspx files

<a href='<%: UrlMaker.ToLogout() %>'>Sign Out</a>
<a href='<%: UrlMaker.ToLogin() %>'>Sign In</a>

<a href='<%: UrlMaker.ToCourse(1234, "course name") %>'>Stackoverflow Tutorial</a>

you could also pass a dynamic parameters from your dynamic data fields like this:

<a href='<%: UrlMaker.ToCourse(Eval("id"), Eval("name")) %>'>Stackoverflow Tutorial</a>

The advantage of the above class is no matter how many times you call a url in different web forms if any changes happen to your urls then you only require to make a change once in your UrlMaker class and it will apply to the entire web forms.

use the above code and let me know if you have any trouble to navigate to different paths.

Ali
  • 2,574
  • 1
  • 17
  • 24