2

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 ??

Ankit
  • 6,388
  • 8
  • 54
  • 79

1 Answers1

0
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];
}
SideFX
  • 839
  • 1
  • 12
  • 34
  • Thanks for the reply. But the reason for the same is that my login page is getting hit twice. First with the expected URL, then again the same url with ClientCompanyName replaced with "iepngfix.htc". This happens on IE – Ankit Apr 06 '11 at 09:54