Stephen Sulzberger’s Blog

January 20, 2008

asp.net: Modify Request.QueryString

Filed under: .net,asp.net,C# — Stephen @ 4:30 pm

Suppose you needed to modify the query string passed in a given URL before redirecting to another (or even the same) page. For example, imagine you needed to take:

/default.aspx?name1=value1&name2=value2&color=red

and turn it into something like:

/default.aspx?name1=valueX&name2=valueY&colorcode=red

I’ve seen this done with loops, new URI’s, and so on to get around the query string read-only “problem”, but most implementations like these are fairly arduous, not to mention error-prone. Given all of the idiosyncrasies involved with the Request.QueryString object (e.g. is read-only, is a modified version of the NameValueCollection object, etc.), I still think the the following approach is the best bet:

using System.Collections.Specialized;

public partial class ModifyQueryStringExample : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    // Create a new (editable) NameValueCollection using the existing (read-only) collection
    NameValueCollection objNewValueCollection = HttpUtility.ParseQueryString(Request.QueryString.ToString());
    objNewValueCollection.Set("name1", "valueX");
    objNewValueCollection.Set("name2", "valueY");
    objNewValueCollection.Add("colorcode", objNewValueCollection.Get("color"));
    objNewValueCollection.Remove("color");

    string sRedirect = Request.Url.AbsolutePath;
    string sNewQueryString = "?" + objNewValueCollection.ToString();

    sRedirect = sRedirect + sNewQueryString;

    Response.Redirect(sRedirect, true);
  }
}

Some notes:

  1. All other parameters passed in by the original query string will be carried over untouched. For example, if the original query string also included something like “id=1234&status=1”, these values would persist into the new redirect even while the other name/value pairs have been modified.    
  2. The NameValueCollection Set() method allows nulls, so it will work regardless of whether or not the key you’re looking for actually exists. If the key exists, the value will be appended accordingly. If the key does not exist, it will be added with the appropriate value. This time around I chose to use the Add() method for readability since I know for a fact that this routine will always be adding a key (“colorcode”) rather than appending one.
  3. All NameValueCollection methods that use a text compare are case-insensitive, as per msdn: http://msdn2.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.namevaluecollection.aspx 
  4. The NameValueCollection ToString() method automatically inserts “=”/”&” characters for you when/where appropriate. The NameValueCollection instance returned by HttpUtility.ParseQueryString is an HTTPValueCollection (a specialized NameValueCollection type), so its ToString() method will automatically insert “=”/”&” characters for you where appropriate. Although this is undoubtedly a nice feature, one can’t help but mention that the surprisingly convenient behavior just described is not immediately obvious given the msdn documentation at http://msdn.microsoft.com/en-us/library/system.web.httputility.parsequerystring.aspx. Nevertheless, the implementation is made clear by evaluating any NameValueCollection.ToString() against an HTTPValueCollection.ToString(). The former will return the object’s string representation (‘System.Collections.Specialized.NameValueCollection’) while the latter, of course, will provide a legitimate name/value collection with the applicable field-value separators. Interestingly, the only way to get an HTTPValueCollection instance in the first place is to use HttpUtility.ParseQueryString. No doubt a quirky matter. For further discussion on the issue, visit http://msmvps.com/blogs/paulomorgado/archive/2008/07/15/make-the-httpvaluecollection-class-public-and-move-it-to-system-dll.aspx               

January 19, 2008

Another example of why a college degree isn’t enough

Filed under: News — Stephen @ 2:11 pm

Good computer science graduates do not make good software developers.

January 12, 2008

Why SWFObject is better than AC_RunActiveContent.js for flash embedding

Filed under: Flash,Web development — Stephen @ 1:41 pm

Some advantages of using SWFObject over Macromedia’s AC_RunActiveContent.js approach (that most places don’t talk about):

  1. SWFObject degrades gracefully. If flash isn’t detected, the “flashcontent” div element never gets replaced with the flash object script. This being the case, you can use xhtml compliant code that’s far cleaner and more robust than anything Macromedia supports out of the box. Interestingly, Macromedia doesn’t even support alternative content when their implementation is called using the jscript. They allow a <noscript> content area, but in that case you’re limited to only having a replacement <img> instead of full-blown replacement content.  
  2. SWFObject entails terser code. When using SFWObject, you’re only placing/calling your flash script once instead of multiple times. Notice, for example, that Macromedia requires the flash object to essentially be placed twice, thereby duplicating your work outright. Needless to say, SFWObject’s code implementation is far more elegant and much easier to maintain.
  3. SWFObject contains an efficient and robust framework that has extensive functionality built right in. As it stands, Macromedia’s implementation has no (or at least an extremely cryptic) method for doing things like, for example, passing flash argument variables. In fact, a quick glance at Macromedia’s AC_RunActiveContent.js file shows that their rendering mechanism is proprietary to the point of absurdity. Case in point, notice Macromedia’s AC_FL_RunContent() function for generating the <embed> object – if that’s not ad hoc, I don’t know what is. Dirty, cumbersome, and the perfect example of unnecessary overhead that bars anything from being readily transparent or extensible.  
  4. And, of course, all of the regular advantages.

Some caveats:

  1. SWFObject doesn’t work if/when javascript is disabled. I personally consider this a small point, but it’s still a valid shortcoming. Of course, it seems to me that browsers that are running without javascript enabled are likely to be running without flash enabled, either; this is hardly the sort of crowd that most web applications (relying on flash, nonetheless) have to cater to. Incidentally, Macromedia’s approach doesn’t suffer this setback given their use of <noscript>. 

EXAMPLE

Anyone familiar with Macromedia’s suggested method for deploying a flash component will find the following typical:

   <script src="app/flash/default3/AC_RunActiveContent.js" type="text/javascript"></script> 

   <script type="text/javascript"> 

     if (AC_FL_RunContent == 0) { 
       alert("This page requires AC_RunActiveContent.js."); 
     } else { 
       AC_FL_RunContent( 
              'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0', 
              'width', '770', 
              'height', '190', 
              'src', 'data/flash/flashObject', 
              'quality', 'high', 
              'pluginspage', 'http://www.macromedia.com/go/getflashplayer', 
              'align', 'middle', 
              'play', 'true', 
              'loop', 'true', 
              'scale', 'showall', 
              'wmode', 'window', 
              'devicefont', 'false', 
              'id', 'flashObject', 
              'bgcolor', '#ffffff', 
              'name', 'flashObject', 
              'menu', 'true', 
              'allowFullScreen', 'false', 
              'allowScriptAccess','sameDomain', 
              'movie', 'data/flash/flashObject', 
              'salign', '', 
              'base', 'data/flash/' 
              ); //end AC code 
          } 

    </script> 

    <noscript> 
       <object id="_objflashObject" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" 
          codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" 
          width="770" height="190" align="middle" type="application/x-shockwave-flash"> 
           <param name="allowScriptAccess" value="sameDomain" /> 
           <param name="allowFullScreen" value="false" /> 
           <param name="movie" value="data/flash/flashObject.swf" /> 
           <param name="quality" value="high" /> 
           <param name="bgcolor" value="#ffffff" /> 
           <param name="base" value="data/flash/" /> 
           <img src="images/flashReplacement.gif" alt="banner" /> 
       </object> 
    </noscript> 

Using SWFObject, one can replace the above using about 1/4 the code:

   <script type="text/javascript" src="data/scripts/swfobject-1.5.js"></script> 

   <div id="flashcontent"> 
     <img src="images/flashReplacement.gif" alt="banner" /> 
   </div> 

   <script type="text/javascript"> 
     var so = new SWFObject("data/flash/flashObject.swf", "flashBanner1", "770", "190", "9", "#fff"); 
     so.addParam("allowScriptAccess", "sameDomain"); 
     so.addParam("base", "data/flash/"); 
     so.write("flashcontent"); 
   </script>

January 9, 2008

define:jscript

Filed under: javascript — Stephen @ 6:30 am

“Microsoft’s bastardization of W3C-approved JavaScript.”
www.metromemetics.com/thelexicon/j.asp

lol…

Source: http://www.google.com/search?hl=en&q=define%3Ajscript

January 8, 2008

Plaxo, Facebook, and Google join DataPortability.org

Filed under: Social — Stephen @ 10:32 pm

It’s…about time. Just read an article about how all of the current as well as up and coming social networking sites could potentially be “locking users in” to their networks since you can’t export your friends, comments, settings, etc (similar to what you would do with exporting/importing buddy lists, email, whatever). Good to see something happening. I guess.

January 6, 2008

Converting VB to C# (and vice versa)

Filed under: asp.net — Stephen @ 7:32 am

I’ve come across a ton of examples and tutorials both online and in print that insist on using and/or providing VB script and nothing more. This really isn’t a big deal considering asp.net makes it super easy to work with consolidated and (for the most part) commonly named directives, methods, and libraries. Even when you don’t have the benefit of common naming, syntax is usually easy to deal with (or, at least, manageable) on a read-only basis when the general flow/concept is made obvious by the context.

But let’s face it – VB just looks ugly. For cases where I have to stare at it for a long time, I like using the free tool at developer fusion to convert VB to C# (incidentally it also converts C# to VB). It’s not perfect, but handy. Probably one of the better convertors out there. 

Create a free website or blog at WordPress.com.