تقديم التحكم في المستخدم:إرسال رابط إلى الصفحة الحالية؟

StackOverflow https://stackoverflow.com/questions/47329

سؤال

أقوم بتنفيذ عنصر تحكم مخصص وفي عنصر التحكم هذا أحتاج إلى كتابة مجموعة من الروابط إلى الصفحة الحالية، كل منها يحتوي على معلمة استعلام مختلفة.أحتاج إلى الحفاظ على سلسلة الاستعلام الحالية كما هي، وإضافة (أو تعديل قيمة) عنصر استعلام إضافي (على سبيل المثال."صفحة"):

"Default.aspx?page=1"
"Default.aspx?page=2"
"Default.aspx?someother=true&page=2"

إلخ.

هل هناك طريقة مساعدة بسيطة يمكنني استخدامها في طريقة Render ...امم ...يحب:

Page.ClientScript.SomeURLBuilderMethodHere(this,"page","1");
Page.ClientScript.SomeURLBuilderMethodHere(this,"page","2");

سيهتم ذلك بإنشاء عنوان URL الصحيح والحفاظ على عناصر سلسلة الاستعلام الحالية وعدم إنشاء نسخ مكررة على سبيل المثال.الصفحة=1&الصفحة=2&الصفحة=3؟

يبدو أن القيام بمهمة غير جذابة.

هل كانت مفيدة؟

المحلول

أخشى أنني لا أعرف أي طريقة مضمنة لذلك، فنحن نستخدم هذه الطريقة التي تأخذ سلسلة الاستعلام وتحدد المعلمات

    /// <summary>
    /// Set a parameter value in a query string. If the parameter is not found in the passed in query string,
    /// it is added to the end of the query string
    /// </summary>
    /// <param name="queryString">The query string that is to be manipulated</param>
    /// <param name="paramName">The name of the parameter</param>
    /// <param name="paramValue">The value that the parameter is to be set to</param>
    /// <returns>The query string with the parameter set to the new value.</returns>
    public static string SetParameter(string queryString, string paramName, object paramValue)
    {
        //create the regex
        //match paramname=*
        //string regex = String.Format(@"{0}=[^&]*", paramName);
        string regex = @"([&?]{0,1})" + String.Format(@"({0}=[^&]*)", paramName);

        RegexOptions options = RegexOptions.RightToLeft;
        // Querystring has parameters...
        if (Regex.IsMatch(queryString, regex, options))
        {
            queryString = Regex.Replace(queryString, regex, String.Format("$1{0}={1}", paramName, paramValue));
        }
        else
        {
            // If no querystring just return the Parameter Key/Value
            if (queryString == String.Empty)
            {
                return String.Format("{0}={1}", paramName, paramValue);
            }
            else
            {
                // Append the new parameter key/value to the end of querystring
                queryString = String.Format("{0}&{1}={2}", queryString, paramName, paramValue);
            }
        }
        return queryString;
    }

من الواضح أنه يمكنك استخدام QueryString NameValueCollection خاصية كائن URI لتسهيل البحث عن القيم، لكننا أردنا أن نكون قادرين على تحليل أي سلسلة استعلام.

نصائح أخرى

ولدينا هذه الطريقة أيضًا التي تسمح لك بوضع سلسلة عنوان URL كاملة دون الحاجة إلى إخراج سلسلة الاستعلام منها

public static string SetParameterInUrl(string url, string paramName, object paramValue)
{
    int queryStringIndex = url.IndexOf("?");
    string path;
    string queryString;
    if (queryStringIndex >= 0 && !url.EndsWith("?"))
    {
        path = url.Substring(0, queryStringIndex);
        queryString = url.Substring(queryStringIndex + 1);
    }
    else
    {
        path = url;
        queryString = string.Empty;
    }
    return path + "?" + SetParameter(queryString, paramName, paramValue);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top