简体   繁体   English

AsP.NET 4.0 URL路由

[英]AsP.NET 4.0 url routing

I am trying the new feature of .NET 4.0 - url routing but not able to fetch information passed in the url. 我正在尝试.NET 4.0的新功能-网址路由,但无法获取在网址中传递的信息。 Following is the code : 以下是代码:

GLOBAL.ASPX.CS 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: 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. 现在,即使我在网址中使用Client / Google,上述属性也会返回null。 When i reset IIS (IIS 6) and do it for first time, it returns value. 当我重新设置IIS(IIS 6)并第一次执行此操作时,它将返回值。 Otherwise it gives null. 否则,它为null。

ANY CLUE ?? 任何线索??

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. 如果未提供URL中的“ ClientCompanyName”值,则RouteValueDictionary用于指示要使用的默认值。 Here, you are saying that you wish "?ClientCompanyName" to be the default. 在这里,您是说希望“?ClientCompanyName”为默认值。 So for example, if you navigated to http://baseUrl/Client , this would default to http://baseUrl/Client/?ClientCompanyName (literally). 因此,例如,如果导航到http:// baseUrl / Client ,则默认为http:// baseUrl / Client /?ClientCompanyName I think you want to actually change ?ClientCompanyName to an actual "real" company name that you wish to be the default. 我认为您想将?ClientCompanyName实际更改为希望作为默认名称的实际“真实”公司名称。 My suggesion would be not to have a default in this case and just use the MapPageRoute override with 3 parameters (string, string, string). 我的建议是在这种情况下不使用默认值,而仅使用具有3个参数(字符串,字符串,字符串)的MapPageRoute覆盖。 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: 此外,这是您可以为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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM