RouteData.Values保持为空
我的路线代码如下:
RouteTable.Routes.MapPageRoute("IDP", "Person/{IDP}", "~/Person.aspx")
现在我想获取表单上的数据,通常它的工作原理如下:
int id = Convert.ToInt32(Page.RouteData.Values["IDP"]);
但每次我试图从路线获取数据,例如: http://PC-81/SkillDatenbank/Person/1
我没有从值中获取数据(它是空的!)
我正在使用ASP.Net 4.5与Web窗体。 编辑:我做了一个新项目并进行了测试,它没有工作要么我做错了什么? 在上一个项目中它确实像这样工作:(你能帮帮我吗?
您的问题可能位于路由表中。 我已经创建了一个WebForms项目来测试它。
打开你的Global.asax
(或者存储你的RouteConfig;默认是App_Start / RouteConfig.cs)并检查你的RegisterRoutes方法。 如果它不存在,请添加以下行。
void RegisterRoutes(RouteCollection routes) { var settings = new FriendlyUrlSettings(); settings.AutoRedirectMode = RedirectMode.Permanent; routes.EnableFriendlyUrls(settings); /* ... additional routes */ routes.MapPageRoute("","Person/{IDP}", "~/Person.aspx"); }
如果在App_Start文件夹中使用默认配置,那么在Application_Start
中应该有这样的一行: RouteConfig.RegisterRoutes(RouteTable.Routes);
如果您在global.asax.cs中定义了路由,则它只是RegisterRoutes(RouteTable.Routes);
如果您现在通过http://PC-81/SkillDatenbank/Person/1
访问您的页面,RouteData字典将包含1组数据,如下所示:
以下代码对我有用,我已将其他路由移至function顶部。
另外,在我的情况下RedirectMode是关闭的,因为我在我的代码中使用Jquery并且为了使Web方法工作,我已经关闭了RedirectMode。
void RegisterRoutes(RouteCollection routes) { /* ... additional routes */ routes.MapPageRoute("","Person/{IDP}", "~/Person.aspx"); var settings = new FriendlyUrlSettings(); settings.AutoRedirectMode = RedirectMode.Permanent; routes.EnableFriendlyUrls(settings); }