8

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?

notAnonymousAnymore
  • 2,637
  • 9
  • 49
  • 74

4 Answers4

1

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?

Ammar Hasan
  • 2,436
  • 16
  • 22
0

Ensure that the UrlRoutingHandler and UrlRoutingModule being registered to IIS in your Web.config are the 4.0 version, and not the 3.5 version. This is the only thing that I can think of which would allow routing to work but your parameters to not map.

Haney
  • 32,775
  • 8
  • 59
  • 68
0

First of all check if it contains the key in your case you can achive this with this line of code

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.RouteData.Values.ContainsKey("q"))
        {
            if (Page.RouteData.Values["q"] != null) 
            {
                Context.Response.Write(Page.RouteData.Values["q"]);
            }
        }
    }

Have you cheked that it is the right route .....????

makemoney2010
  • 1,222
  • 10
  • 11
-1

I had same problem and I got this resolved by adding following under the configuration section of web.config:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
Shahid
  • 27
  • 1