5

My Code for the Route looks like this:

 RouteTable.Routes.MapPageRoute("IDP", "Person/{IDP}", "~/Person.aspx")

And now i want to get the Data on a Form, normally it works like this:

int id = Convert.ToInt32(Page.RouteData.Values["IDP"]);

But everytime i try to get the Data from the Route e.g.: http://PC-81/SkillDatenbank/Person/1 I get no Data from the Value (it is empty!)

I am using ASP.Net 4.5 with Web Forms. Edit: I made an new Project and tested and it didnt work either What am i Doing wrong? in the Last Project it did work like this :( Can you help me?

djdiox
  • 75
  • 1
  • 7
  • Where do you assign your `id` variable? On `Page_Load`, afterwards, before? – Marco Jul 08 '14 at 13:33
  • Right in the page_Load on the beginning Edit: i did the method in a button and tried it then. But still in `code`Page.RouteData.Values["ID"]; is nothing at all – djdiox Jul 08 '14 at 13:33

2 Answers2

4

Following code has worked for me, I have moved additional routes to top of the function.

Also, in my case RedirectMode is off because I'm using Jquery in my code and inorder to make a Webmethod work, I have turned off RedirectMode.

void RegisterRoutes(RouteCollection routes)
{
     /* ... additional routes */
    routes.MapPageRoute("","Person/{IDP}", "~/Person.aspx");

    var settings = new FriendlyUrlSettings();
    settings.AutoRedirectMode = RedirectMode.Permanent;
    routes.EnableFriendlyUrls(settings);

}
  • 1
    Yes - calling MapPageRoute **before** calling EnableFriendlyRoutes() solved it for me. Calling MapPageRoute **after** resulted in Page.RouteData.Values[] being empty – dlchambers Apr 08 '17 at 15:19
3

Your problem ist probably located in your Routing table. I have created a WebForms project to test this out.

Open up your Global.asax (or whereever you store your RouteConfig; default is App_Start/RouteConfig.cs) and check your RegisterRoutes method. Add the following line to it, if it isn't already present.

void RegisterRoutes(RouteCollection routes)
{
    var settings = new FriendlyUrlSettings();
    settings.AutoRedirectMode = RedirectMode.Permanent;
    routes.EnableFriendlyUrls(settings);

    /* ... additional routes */
    routes.MapPageRoute("","Person/{IDP}", "~/Person.aspx");
}

In your Application_Start there should be a line like this, if you use the default configuration with in the App_Start folder: RouteConfig.RegisterRoutes(RouteTable.Routes);

If you defined your routes in your global.asax.cs it is just RegisterRoutes(RouteTable.Routes);

If you now access your Page via http://PC-81/SkillDatenbank/Person/1 the RouteData dictionary will contain 1 set of data, like this:

enter image description here

Marco
  • 22,856
  • 9
  • 75
  • 124
  • Thanks a lot this has worked! anyway it's weird that the other way worked in my other project at all, but i am fine with that! – djdiox Jul 08 '14 at 14:10
  • 1
    I've done exactly this and still get values.count = 0 – Jarrette Oct 08 '16 at 15:16
  • 2
    answer to my issue is here: http://stackoverflow.com/questions/20746022/page-routedata-values-are-empty-for-one-page-but-not-another You can't use a routing name that is an actual page name – Jarrette Oct 08 '16 at 16:48
  • Not entirely true, see the other answer for the solution. But it kinda solves the issue as well. – Marco Luglio Sep 22 '17 at 13:14