0

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.

James00
  • 3
  • 2

2 Answers2

3

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('.');
ekolis
  • 6,270
  • 12
  • 50
  • 101
  • Minor improvement: better introduce variable for Request.QueryString["speakers"] so you wouldn't calculate it twice. – Dmitry Osinovskiy Jul 15 '12 at 14:15
  • Sorry, I am not very experienced with C#. I am attempting ekolis's answer, but it not working for me. I may not have all of the syntax correct? Is ekolis's code supposed to be within the curly brackets of public string myspeakers() { string[] speakers; if... – James00 Jul 15 '12 at 19:45
  • Yes, it should be in the curly brackets of the myspeakers() method. What error are you getting? You will need to change the return type of myspeakers() from string to string[]... – ekolis Jul 15 '12 at 20:44
  • I'm getting compile error, because I am not using the proper syntax. Would it be possible for you to edit the answer with the full method? I am not sure how to put together the return... inside the curly brackets. I have tried several ways, but its not working for me. I am trying to read through documentation but I am just not finding it. – James00 Jul 15 '12 at 22:17
  • `return speakers;` gives me an error "Cannot implicitly convert type 'string[]' to 'string' – James00 Jul 16 '12 at 13:31
  • You'll need to change the return type of the method from string to string[], since it's now going to be returning an array. – ekolis Jul 16 '12 at 13:37
  • how would you type it? I have been trying several different ways without success. `void myspeakers() { string[] speakers; if (Request.QueryString["speakers"] == null) { speakers = new string [0]; } else { speakers = Request.QueryString["speakers"].Split('.'); } return string[]; }` – James00 Jul 16 '12 at 14:30
  • void means there is nothing returned from the function; you want public string[] myspeakers() {...} – ekolis Jul 16 '12 at 19:53
1

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('.');
    } 
}
andres descalzo
  • 14,887
  • 13
  • 64
  • 115