问题描述
我对路由代码如下所示:
RouteTable.Routes.MapPageRoute(IDP,人/ { IDP},〜/ Person.aspx)
,现在我想在拿到数据一种形式,通常它的工作原理是这样的:
INT ID = Convert.ToInt32(Page.RouteData.Values [IDP] );
但每次我试图从路线如获得数据: HTTP: // PC-81 / SkillDatenbank /人/ 1
我没有得到任何来自值(它是空的!)
数据我使用ASP.Net 4.5与Web窗体。
编辑:我做了一个新的项目和测试,它没有工作,要么
我究竟做错了什么?在最后一个项目它确实是这样:(
的工作,你能帮我吗?
您的问题可能IST位于在你的路由表。我创建了一个WebForms的项目来测试这一点。
打开你的的Global.asax
(或徘徊无论您存储RouteConfig;默认为App_Start / RouteConfig.cs),并检查您的RegisterRoutes方法如下行添加到它,如果它不存在。
无效的RegisterRoutes(RouteCollection路线)
{
VAR设置=新FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(设置);
/ * ...其他路由* /
routes.MapPageRoute(,人/ {} IDP,〜/ Person.aspx );
}
在你的的Application_Start
应该有这样一条线,如果在App_Start文件夹使用默认的配置有: RouteConfig.RegisterRoutes(RouteTable.Routes);
如果您在您的global.asax.cs定义你的路由,这只是的RegisterRoutes(RouteTable.Routes);
如果您现在可以通过 HTTP访问你的页面:// PC-81 / SkillDatenbank /人/ 1
中的RouteData字典将包含1套数据是这样的:
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 eitherWhat am i Doing wrong? in the Last Project it did work like this :(Can you help me?
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:
这篇关于RouteData.Values保持为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!