0

いくつかのurlパラメータを取得した後、後でc#コードで使用したいと思いますが、形式が少し異なります。

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

c#コードを使用して、既存の形式の文字列として値を取得できます。

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

myattendeesが戻ります(引用符なしのピリオドで区切られます)

john.sam

およびmyspeakersが戻ります(引用符なしのピリオドで区切られます)

fred.will.tony.amy.al

しかし、コンマで区切られた一重引用符で囲まれた値を持つこれらのような文字列を返すように変換したいと思います。

'ジョン'、'サム'

'フレッド'、'ウィル'、'トニー'、'エイミー'、'アル'

C#でこれを行うための最良の方法は何でしょうか?NameValueCollectionを使用しますか?

*詳細を明確にするために編集された質問。*編集-スペルミスを修正しました。

4

2 に答える 2

3

このコードは、ドットで分割することによって取得された文字列の配列を提供します。

string[] speakers;
if (Request.QueryString["speakers"] == null)
    speakers = new string[0];
else
    speakers = Request.QueryString["speakers"].Split('.');
于 2012-07-15T14:08:52.347 に答える
1

これを試して:

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('.');
    } 
}
于 2012-07-15T14:14:52.500 に答える