Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Constructing a URL?

Constructing a URL?

Scheduled Pinned Locked Moved C#
questionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fireloard
    wrote on last edited by
    #1

    Which would be considered better practice? Although the first version is smaller the second one uses string builder and breaks it up so that it is easier to distinguish what is being added to the line and avoids hard coding in the actual program. So which would you normally do? Both of these create the same URL in the end. Stringing it all together like this? string strRedirect = "~/Maint/surcharge.aspx?" + "StartDate=" + cpStartDate.SelectedDate.ToShortDateString() + "&EndDate=" + cpEndDate.SelectedDate.ToShortDateString() + "&Market=" + ddlMarkets.SelectedIndex + "&GroupName=" + cbCustomer.Checked.ToString() + "&GroupCode=" + cbGroupCode.Checked.ToString() + "&Abbreviation=" + cbAbbreviation.Checked.ToString(); Or using a string builder to create the URL like this: private const string URL_SURCHARGE = "~/Maint/homepage.aspx"; private const string STR_ASP_DELIMITER = "&"; private const string STR_ASP_PARMS = "?"; private const string STR_ASP_EQUALS = "="; private const string DF_START_DATE = "StartDate"; private const string DF_END_DATE = "EndDate"; private const string DF_GROUP_CODE = "GroupCode"; private const string DF_ABBREVIATION = "Abbreviation"; private const string DF_GROUP_NAME = "GroupName"; private const string DF_MARKET_TYPE = "Market"; StringBuilder sbRedirect = new StringBuilder( URL_SURCHARGE ); // Add ASP parameter delimiter. Should be a ? sbRedirect.Append( STR_ASP_PARMS ); // Start Date sbRedirect.Append( DF_START_DATE ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( cpStartDate.SelectedDate.ToShortDateString() ); sbRedirect.Append( STR_ASP_DELIMITER ); // End Date sbRedirect.Append( DF_END_DATE ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( cpEndDate.SelectedDate.ToShortDateString() ); sbRedirect.Append( STR_ASP_DELIMITER ); // Market Type sbRedirect.Append( DF_MARKET_TYPE ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( ddlMarkets.SelectedIndex ); sbRedirect.Append( STR_ASP_DELIMITER ); // Group Name sbRedirect.Append( DF_GROUP_NAME ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( cbCustomer.Checked.ToString() ); sbRedirect.Append( STR_ASP_DELIMITER ); // Group Name sbRedirect.Append( DF_GROUP_CODE ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( cbGroupCode.Checked.ToString() ); sbRedirect.Append( STR_ASP_DELIMITER ); // Abbreviation sbRedirect.Append( DF_GROUP_CODE ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( cbAbbrevi

    C 1 Reply Last reply
    0
    • F fireloard

      Which would be considered better practice? Although the first version is smaller the second one uses string builder and breaks it up so that it is easier to distinguish what is being added to the line and avoids hard coding in the actual program. So which would you normally do? Both of these create the same URL in the end. Stringing it all together like this? string strRedirect = "~/Maint/surcharge.aspx?" + "StartDate=" + cpStartDate.SelectedDate.ToShortDateString() + "&EndDate=" + cpEndDate.SelectedDate.ToShortDateString() + "&Market=" + ddlMarkets.SelectedIndex + "&GroupName=" + cbCustomer.Checked.ToString() + "&GroupCode=" + cbGroupCode.Checked.ToString() + "&Abbreviation=" + cbAbbreviation.Checked.ToString(); Or using a string builder to create the URL like this: private const string URL_SURCHARGE = "~/Maint/homepage.aspx"; private const string STR_ASP_DELIMITER = "&"; private const string STR_ASP_PARMS = "?"; private const string STR_ASP_EQUALS = "="; private const string DF_START_DATE = "StartDate"; private const string DF_END_DATE = "EndDate"; private const string DF_GROUP_CODE = "GroupCode"; private const string DF_ABBREVIATION = "Abbreviation"; private const string DF_GROUP_NAME = "GroupName"; private const string DF_MARKET_TYPE = "Market"; StringBuilder sbRedirect = new StringBuilder( URL_SURCHARGE ); // Add ASP parameter delimiter. Should be a ? sbRedirect.Append( STR_ASP_PARMS ); // Start Date sbRedirect.Append( DF_START_DATE ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( cpStartDate.SelectedDate.ToShortDateString() ); sbRedirect.Append( STR_ASP_DELIMITER ); // End Date sbRedirect.Append( DF_END_DATE ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( cpEndDate.SelectedDate.ToShortDateString() ); sbRedirect.Append( STR_ASP_DELIMITER ); // Market Type sbRedirect.Append( DF_MARKET_TYPE ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( ddlMarkets.SelectedIndex ); sbRedirect.Append( STR_ASP_DELIMITER ); // Group Name sbRedirect.Append( DF_GROUP_NAME ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( cbCustomer.Checked.ToString() ); sbRedirect.Append( STR_ASP_DELIMITER ); // Group Name sbRedirect.Append( DF_GROUP_CODE ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( cbGroupCode.Checked.ToString() ); sbRedirect.Append( STR_ASP_DELIMITER ); // Abbreviation sbRedirect.Append( DF_GROUP_CODE ); sbRedirect.Append( STR_ASP_EQUALS ); sbRedirect.Append( cbAbbrevi

      C Offline
      C Offline
      cobyjone
      wrote on last edited by
      #2

      The best of both worlds: sbRedirect.AppendFormat("~/Maint/surcharge.aspx?StartDate={0}&EndDate=(1)&Market={2}&GroupName={3}&GroupCode={4}&Abbreviation={5}", cpStartDate.SelectedDate.ToShortDateString(), cpEndDate.SelectedDate.ToShortDateString(), ddlMarkets.SelectedIndex, cbCustomer.Checked.ToString(), cbGroupCode.Checked.ToString(), cbAbbreviation.Checked.ToString());

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups