Question

After getting some url parameters, I would like to use them later in my c# code, but in a slightly different format.

"http://example/myexample.aspx?attendees=john.sam&speakers=fred.will.tony.amy.al"

I am able to get the values as a string in their existing format using: c# code

public string myattendees()
{
    string attendees;
    attendees = Request.QueryString["attendees"];
    return attendees;
}
public string myspeakers()
{
    string speakers;
    speakers = Request.QueryString["speakers"];
    return speakers;
}

myattendees returns (separated with periods with no quotes)

john.sam

and myspeakers returns (separated with periods with no quotes)

fred.will.tony.amy.al

But I would like to convert it so it would return a string like these with comma separated and single quoted values.

'john' , 'sam'

and

'fred' , 'will' , 'tony' , 'amy' , 'al'

What would be the best way to do this in c#? Use a NameValueCollection?

*question edited for clarity on details. *edit - fixed spelling error.

Was it helpful?

Solution

This code will give you an array of strings acquired by splitting on dots:

string[] speakers;
if (Request.QueryString["speakers"] == null)
    speakers = new string[0];
else
    speakers = Request.QueryString["speakers"].Split('.');

OTHER TIPS

try this:

public class MyClassGetQueryString
{

    private const string Attendees = "attendees";
    private const string Speakers = "speakers";

    public string MyAttendees()
    {
        return Request.QueryString[MyClassGetQueryString.Attendees] ?? string.Empty;
    }

    public string MySpeakers()
    {
        return Request.QueryString[MyClassGetQueryString.Speakers] ?? string.Empty;
    }

    public string[] MyAttendeesParts()
    {
        return this.MyAttendees().Split('.');
    } 

    public string[] MySpeakersParts()
    {
        return this.MySpeakers().Split('.');
    } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top