AsP.NET 4.0 url 路由
我正在尝试 .NET 4.0 的新功能 - url 路由,但无法获取 url 中传递的信息。以下是代码:
GLOBAL.ASPX.CS
protected void Application_Start(object sender, EventArgs e)
{
SetRouting(RouteTable.Routes);
}
private void SetRouting(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("Company",
"Company/{CompanyName}",
"~/Asset/RequestForm.aspx", true, new RouteValueDictionary { { "CompanyName", "?CompanyName" } });
routeCollection.MapPageRoute("Deal",
"Company/{CompanyName}/{DealName}",
"~/Asset/RequestForm.aspx", true, new RouteValueDictionary { { "DealName", "?DealName" } });
routeCollection.MapPageRoute("ClientRoute",
"Client/{ClientCompanyName}",
"~/User/Login.aspx", true, new RouteValueDictionary { { "ClientCompanyName", "?ClientCompanyName" } });
}
Login.aspx:
private string CompanyName {
get
{
if (Page.RouteData.Values["ClientCompanyName"] == null)
{
return null;
}
return Page.RouteData.Values["ClientCompanyName"].ToString();
}
}
现在,即使我在 url 中使用 Client/Google,上述属性也会返回 null。当我重置 IIS (IIS 6) 并第一次执行此操作时,它会返回值。否则它给出 null。
有什么线索吗?
I am trying the new feature of .NET 4.0 - url routing but not able to fetch information passed in the url. Following is the code :
GLOBAL.ASPX.CS
protected void Application_Start(object sender, EventArgs e)
{
SetRouting(RouteTable.Routes);
}
private void SetRouting(RouteCollection routeCollection)
{
routeCollection.MapPageRoute("Company",
"Company/{CompanyName}",
"~/Asset/RequestForm.aspx", true, new RouteValueDictionary { { "CompanyName", "?CompanyName" } });
routeCollection.MapPageRoute("Deal",
"Company/{CompanyName}/{DealName}",
"~/Asset/RequestForm.aspx", true, new RouteValueDictionary { { "DealName", "?DealName" } });
routeCollection.MapPageRoute("ClientRoute",
"Client/{ClientCompanyName}",
"~/User/Login.aspx", true, new RouteValueDictionary { { "ClientCompanyName", "?ClientCompanyName" } });
}
Login.aspx:
private string CompanyName {
get
{
if (Page.RouteData.Values["ClientCompanyName"] == null)
{
return null;
}
return Page.RouteData.Values["ClientCompanyName"].ToString();
}
}
Now the property mentioned above returns null even when i use Client/Google in the url. When i reset IIS (IIS 6) and do it for first time, it returns value. Otherwise it gives null.
ANY CLUE ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这其实没有多大意义。 RouteValueDictionary 用于指示在未提供 URL 中的“ClientCompanyName”值的情况下要使用的默认值。在这里,您希望“?ClientCompanyName”成为默认值。例如,如果您导航到 http://baseUrl/Client,则默认为 http://baseUrl/Client/?ClientCompanyName (字面意思)。我认为您实际上想将 ?ClientCompanyName 更改为您希望作为默认值的实际“真实”公司名称。我的建议是在这种情况下不要使用默认值,而只使用带有 3 个参数(字符串、字符串、字符串)的 MapPageRoute 覆盖。我发现在很多情况下这就足够了。另外,这里有一个可以帮助您的 Request 对象的扩展方法:
This actually doesn't make much sense. The RouteValueDictionary is used to indicate the default value to use given that the "ClientCompanyName" value in the URL is not provided. Here, you are saying that you wish "?ClientCompanyName" to be the default. So for example, if you navigated to http://baseUrl/Client, this would default to http://baseUrl/Client/?ClientCompanyName (literally). I think you want to actually change ?ClientCompanyName to an actual "real" company name that you wish to be the default. My suggesion would be not to have a default in this case and just use the MapPageRoute override with 3 parameters (string, string, string). I found that in many instances this is enough. Also, here is an extension method that you can you for the Request object that may help you: