Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page.RouteData.Values["parameter"] not working

I created a new Web Forms (ASP.NET 4.5) project and I'm trying to get URL routing to work.

RouteConfig.cs looks like this:

routes.MapPageRoute("surveyhome", "survey/home", "~/Survey.aspx");
routes.MapPageRoute("surveyquestions", "survey/questions/{q}", "~/Survey.aspx");

I have a link that looks like this:

<a href="/survey/questions/1">1</a>

It correctly loads the Survey.aspx page, so I know it's partly working, but this code (in the codebehind of that page) doesn't work:

if (Page.RouteData.Values["q"] != null)
{
    // do something
}

It's always null. Why?

like image 254
notAnonymousAnymore Avatar asked Jun 02 '13 20:06

notAnonymousAnymore


1 Answers

I have tested your code, and no problems for me on ASP.NET 4.0.

Try defining default values, like

routes.MapPageRoute("surveyquestions",
    "survey/questions/{q}", "~/Survey.aspx",
    false,
    new RouteValueDictionary 
        { { "q", String.Empty } });

Did that resolve your problem?

like image 139
Ammar Hasan Avatar answered Oct 20 '22 00:10

Ammar Hasan