Uri.Query, Url parsing, SEO, standard implementation of url query parsers

Discussion in 'C#' started by nubsii, May 6, 2008.

  1. #1
    I've implemented all my features while trying to avoid any .NET modules/wizards, and I would really appreciate a sanity check as well as suggestions for making things better. Also, this is my first time working with info baked into the URL and I think I might be making it more complicated than it needs to be.

    I have an existing feature, which is an email verification which occurs at sign-up. Here's a slimmed down version of the logic + important code lines for this feature.
    Create a verification link in an email
    
    //the rest of the email was created before this line
    string uid = GetActivationKey(24);
    mailItem.Body = "Please click the following link to activate your account: " + 
         "http://www.domain.com/Activation.aspx?uid=" + uid;
    //from here I store the uid and send this email
    
    Code (markup):
    The random key generator (based on 4guysfromrolla's coverage of the topic):
    
        public string GetActivationKey(int length)
        {
            string guid = System.Guid.NewGuid().ToString();
            guid = guid.Replace("-", string.Empty);
            //How long are GUIDs...?
            return guid.Substring(0, length);
        }
    
    Code (markup):
    After the new user clicks the link and comes back, the URL looks something like this: domain/Activation.aspx?uid=46d997e2a5444aec933caf09

    There's probably an elegant way to handle this, (which i would love to know..) but what I do is search for the "=" and then take the rest.
    
        protected void Page_Load(object sender, EventArgs e)
        {
            string urlQuery = HttpContext.Current.Request.Url.Query;
            Response.Write(GetActivationKeyFromUrl(urlQuery));
            //in reality i compare the key to the key i originally gave out, and then activate the user's account
        }
    
        public string GetActivationKeyFromUrl(string urlOrQuery)
        {
            string activationKey;
            for (int i = 0; i < urlOrQuery.Length; i++)
            {
                if (urlOrQuery[i] == '=')
                {
                    activationKey = 
                        urlOrQuery.Substring(i + 1, urlOrQuery.Length - (i + 1));
                    return activationKey;
                }
            }
            //exception goes here, may or may not be a good idea, removed for brevity in any case
        }
    
    Code (markup):
    Everything works fine, but it feels hackish and I'm unexcited about extending it to a CMS which I'm making. I'll need to handle urls that look like: something.aspx?topic=blah&sort=asc&filter=whatever&reality=imSureTheresAlreadyAwayToDoThis

    Furthermore I'm considering a more SEO-friendly URL implementation of that example URL i just wrote..whereby the various query pieces are would fall into something that looks more like a file structure. Stuff like zoo.aspx&area=mammals&subarea=canines&furcolor=black would become: zoo/mammals/canines/black-fur.aspx (well i probably wouldn't take it that far, but you get my point)

    Your words of wisdom on my existing code and how to extend its functionality would be much appreciated! (esp if you can tell me how you do this in your applications)

    Left to my own devices I would just start parsing with something like "&(.*?)=(.*?)&?"
     
    nubsii, May 6, 2008 IP
  2. dgxshiny

    dgxshiny Greenhorn

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    23
    #2
    I think you are just looking for Request.QueryString[""] function

    For the URL: something.aspx?topic=blah&sort=asc&filter=whatever&reality=imSureTheresAlreadyAwayToDoThis

    string Topic = Request.QueryString["topic"].ToString();
    string Sort = Request.QueryString["sort"].ToString();
    string Filter = Request.QueryString["filter"].ToString();
    string Reality = Request.QueryString["reality"].ToString();
    Code (markup):
    Or you can access them by index, Request.QueryString[0].ToString(), Request.QueryString[1].ToString(), etc

    I should also note that it is good practice to make sure they are not null in order to prevent an exception.

    if(Request.QueryString["topic"] != null)
    {
         string Topic = Request.QueryString["topic"].ToString();
    }
    else
    {
         //handle missing data
    }
    Code (markup):
     
    dgxshiny, May 6, 2008 IP
    vpguy likes this.
  3. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Classic! :p
     
    vpguy, May 7, 2008 IP
  4. nubsii

    nubsii Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks a ton dgxshiny :)
    that is exactly what i needed, and thx for the heads up about nulls

    and to vpguy:
    lol, indeed.

    Regarding SEO friendliness:
    So does anyone have anything to say about turning these query-filled URLs into SEO friendly URLs? I've read a bit about URL rewriting and such, as well as virtual pages. From what I can tell there are a few ways to turn page.aspx?audience=googlebot&todo=unlistMySite into page/googlebot/unlistMySite.aspx (a mere example) but I've read horror stories about poor implementations causing undesired SEO implications despite the links looking 'pretty.'
    Does anyone have a product with SEO-friendly links implemented and some info to share? (and do you use anything to analyze your site and see if its giving out 302s and such?)
     
    nubsii, May 7, 2008 IP
  5. dgxshiny

    dgxshiny Greenhorn

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    23
    #5
    For my company's blog I use a simple URL rewrite to refer to the blog posts.

    Instead of using the url /view/Default.aspx?BlogPostID=101 or something, the URL is /blog/the-employee-difference.aspx. It is completely transparent to the user, the search engines index the pages by the title and it works out well.

    To see it in action:
    http://www.americasbestcompanies.com/blog/the-employee-difference.aspx


    There are many ways to do it, the module that I use in this instant is called URL Rewrite: http://www.urlrewriting.net/en/Default.aspx
     
    dgxshiny, May 7, 2008 IP
  6. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    URL rewriting with IIS is a bit of a pain. The "built-in" ASP.NET rewriting capabilities are 95% useless.

    With ASP.NET, the URL rewriting "feature" is at the ASP layer, not the IIS layer. This means that whatever URL is entered by the user needs to meet both of the following criteria:
    (1) It has to point to a page that ASP would process (namely, an aspx file), and
    (2) That file has to exist, otherwise a 404 error will be returned.

    For example, if you are trying to capture this SEO-friendly URL:

    http://www.domain.com/products/friendly-product-name.aspx

    ... and process it as this URL:

    http://www.domain.com/product.aspx?friendly-product-name

    ... then you need to have a /products/ folder, a default.aspx file within that folder (even if it is zero bytes), and you may need a file named friendly-product-name.aspx

    I have had mixed (inconsistent) results with requiring files to exist. Sometimes it needs them, sometimes it doesn't, and there doesn't seem to be any pattern.

    If you can find something which interacts with user-entered URL's at the IIS level, that would be the best solution.
     
    vpguy, May 7, 2008 IP