RouteData.Values 保持为空 [英] RouteData.Values stays Empty
问题描述
我的路线代码如下所示:
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"]);
但每次我尝试从路线获取数据时,例如:http://PC-81/SkillDatenbank/Person/1
我从值中没有得到任何数据(它是空的!)
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!)
我将 ASP.Net 4.5 与 Web 窗体一起使用.我创建了一个新项目并进行了测试,但它也不起作用我究竟做错了什么?在上一个项目中它确实是这样工作的:(你能帮我吗?
I am using ASP.Net 4.5 with Web Forms. 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?
推荐答案
您的问题可能出在您的路由表中.我创建了一个 WebForms 项目来对此进行测试.
Your problem ist probably located in your Routing table. I have created a WebForms project to test this out.
打开您的 Global.asax
(或您存储 RouteConfig 的任何位置;默认为 App_Start/RouteConfig.cs)并检查您的 RegisterRoutes 方法.向其中添加以下行(如果尚不存在).
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");
}
在您的 Application_Start
中应该有这样一行,如果您使用 App_Start 文件夹中的默认配置:RouteConfig.RegisterRoutes(RouteTable.Routes);
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);
如果您在 global.asax.cs 中定义了路由,则它只是 RegisterRoutes(RouteTable.Routes);
If you defined your routes in your global.asax.cs it is just RegisterRoutes(RouteTable.Routes);
如果您现在通过 http://PC-81/SkillDatenbank/Person/1
访问您的主页,RouteData 字典将包含 1 组数据,如下所示:
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 保持为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!