RouteData.Values stays Empty

2019-02-18 10:01发布

问题:

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?

回答1:

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:



回答2:

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);

}


标签: c# web routes