Constructing a URL?
-
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
-
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
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());