AsP.NET 4.0 url 路由

发布于 2024-10-30 08:09:15 字数 1336 浏览 0 评论 0原文

我正在尝试 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2024-11-06 08:09:15
routeCollection.MapPageRoute("ClientRoute", 
                             "Client/{ClientCompanyName}",         
                             "~/User/Login.aspx", 
                             true, 
                             new RouteValueDictionary {{ "ClientCompanyName", "?ClientCompanyName"}};

这其实没有多大意义。 RouteValueDictionary 用于指示在未提供 URL 中的“ClientCompanyName”值的情况下要使用的默认值。在这里,您希望“?ClientCompanyName”成为默认值。例如,如果您导航到 http://baseUrl/Client,则默认为 http://baseUrl/Client/?ClientCompanyName (字面意思)。我认为您实际上想将 ?ClientCompanyName 更改为您希望作为默认值的实际“真实”公司名称。我的建议是在这种情况下不要使用默认值,而只使用带有 3 个参数(字符串、字符串、字符串)的 MapPageRoute 覆盖。我发现在很多情况下这就足够了。另外,这里有一个可以帮助您的 Request 对象的扩展方法:

public static string GetDataFromRouteOrRequest(this HttpRequest request, string key)
{
    if (request.RequestContext.RouteData.Values.ContainsKey(key))
        return request.RequestContext.RouteData.Values[key].ToString();

    return request[key];
}
routeCollection.MapPageRoute("ClientRoute", 
                             "Client/{ClientCompanyName}",         
                             "~/User/Login.aspx", 
                             true, 
                             new RouteValueDictionary {{ "ClientCompanyName", "?ClientCompanyName"}};

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:

public static string GetDataFromRouteOrRequest(this HttpRequest request, string key)
{
    if (request.RequestContext.RouteData.Values.ContainsKey(key))
        return request.RequestContext.RouteData.Values[key].ToString();

    return request[key];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文