Calvin Belden
Calvin Belden

Reputation: 3104

GetRouteUrl returning correct url in code behind but null in .aspx

I can correctly get at URL in a code-behind file, but the same statement returns nothing in an inline statement.

Here is part of my RegisterCustomRoutes method, which gets executed in Application_Start:

void RegisterCustomRoutes(RouteCollection routes) {
     // Student list route
     routes.MapPageRoute(
          "StudentListRoute",
          "Course/{courseId}/Students",
          "~/path/to/page.aspx");

      // Course details route
      routes.MapPageRoute(
          "CourseDetailsRoute",
          "Course/{courseId}",
          "~/path/to/otherpage.aspx");

      ... some other route declarations
}

Then, in one of my .aspx pages, I have the following inline statement:

<a href="<%# GetRouteURL("CourseDetailsRoute", new { courseId = 1}) %>">Some text</a>

I am expecting the generated URL to be:

/Course/1

Instead, nothing (either null or string.Empty) is returned. Again, I have confirmed that this statement correctly returns the desired URL in the code behind file.

Upvotes: 0

Views: 744

Answers (1)

Calvin Belden
Calvin Belden

Reputation: 3104

My inline statement used the <%# ... %> tags, which are reserved for databinding purposes. Changed my inline statement to,

<a href="<%: GetRouteUrl("RouteName", new { param = paramValue }) %>">Anchor text</a>

which resolved my issues.

Upvotes: 1

Related Questions