Page.RouteData.Values are empty for one page but n

2020-02-12 13:42发布

问题:

I have Routing working in ASP.NET c# WebForms using Microsoft.AspNet.FriendlyUrls but not for all pages.

Here is an example:

routes.MapPageRoute("List/{Location}/{ZipCode}/", "List/{Location}/{ZipCode}/", "~/List.aspx");

On the above mentioned page (List.aspx) in the page_load there are no values.count in the Page.RouteData.

Page.RouteData.Values.Count == 0

I have another page in the same site with this info matched to it:

routes.MapPageRoute("{Location}/{ZipCode}/{Name}/{LocID}/{ID}/{Code}/", "{Location}/{ZipCode}/{Name}/{LocID}/{ID}/{Code}/", "~/place.aspx");

This page (place.aspx) always shows the correct count of Routes.

While on the List page in debug mode I checked the querystring and location and the ZipCode were there.

So, what might cause Page.RouteData to not show in one page but be available in another?

回答1:

I am pretty new to url routing, but I think I ran into a similar problem and also found the solution. Have you tried:

routes.MapPageRoute("ListDetails/{Location}/{ZipCode}/", "ListDetails/{Location}/{ZipCode}/", "~/List.aspx");

instead of

routes.MapPageRoute("List/{Location}/{ZipCode}/", "List/{Location}/{ZipCode}/", "~/List.aspx");

?

In my case I had:

routes.MapPageRoute(
    "ImageDelete",
    "Admin/Images/Delete/{nameToKill}",
    "~/Admin/Images.aspx"
);

which showed Page.RouteData.Values always empty.

When I changed it to

routes.MapPageRoute(
    "ImageDelete",
    "Admin/Image/Delete/{nameToKill}", // mind the missing 's'
    "~/Admin/Images.aspx"
);

it worked!

I think the route may not start with the URL of a site that actually exists.