1

我制作了一个辅助方法,使用它来修改查询字符串,用于测试和调试

现在唯一的问题是我想在我的 C# 集合助手类中将它作为一个类

该方法现在运行良好,但因为我仍然是“新鲜”的 .net 开发人员

当它位于单独的公共静态或非静态类中时,我无法弄清楚如何使用它 this.Request.QueryString来访问当前表单(form1)值partial class

这是代码,你可以自由使用它(:

public void QsModify(string action, string NewP_Value, string CurQS_ParamName, string         NewQs_paramName=null, bool redirectWithNewQuerySettings=false)
{

#region <<=========== reflect to readonly & set QueriString ReadOnly - false ============>>





// reflect to readonly property 
PropertyInfo isReadOnly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

// make collection editable 
isReadOnly.SetValue(this.Request.QueryString, false, null);

#endregion
switch (action)
{
    case ModAQs_Remove:
        if (notEmptyQs()) 
        this.Request.QueryString.Remove(CurQS_ParamName);
        break;
    case ModAQs_Replace:
        this.Request.QueryString.Remove(CurQS_ParamName);
        this.Request.QueryString.Add(NewQs_paramName, NewP_Value);
        break;
    case ModAQs_EditValue:
        this.Request.QueryString.Set(CurQS_ParamName, NewP_Value);
        break;

    case ModAQs_Add:
        this.Request.QueryString.Add(NewQs_paramName, NewP_Value);
        break;

}


#region <<=========== joining Full Path & queryString then Redirection  ============>>


string seperator = "?";
UrlPath = Request.Url.AbsolutePath;
UrlPathAndQuery = string.Join(seperator, UrlPath, this.Request.QueryString);
isReadOnly.SetValue(this.Request.QueryString, true, null);

if (redirectWithNewQuerySettings)
{

    Response.Redirect(UrlPathAndQuery);
}
#endregion
}

我如何使它成为一个能够访问我正在处理的任何项目的类,但不知道表单名称是什么

4

1 回答 1

1

您可以使用

HttpContext.Current.Request.QueryString["qs_key"];

您可以在静态或非静态上下文中使用它。

于 2012-09-28T00:45:57.923 回答