改变QueryString参数/值

时间:2012-09-22 15:00:41

标签: asp.net c#-4.0 request query-string asp.net-4.0

我想更好地了解为网址设置新参数的问题 并通过

检索它

var ParaValue = Request.QueryString["parameterName"];

所以,如果我有一个网址:“http://www.myWebsite.aspx?UserName = Alice”

我将通过上面的例子检索它

string uName = Request.QueryString["UserName"].ToString();

但如果我想改变价值怎么办?使UserName =“Ralf”

  • 重新编辑

按下按钮时 有一个参数“state”,它包含对按下按钮的引用 国家的价值是=“无” 现在我想将它设置为img_button1。

我甚至没有发送实际的imgbutton id

我很难将其编码为测试/引用

所以我可以知道我正处于事件的阶段 button1

img_button2

触发事件时

我想将状态设置为“img_button2” 等“

3 个答案:

答案 0 :(得分:3)

在我完成研究之后(我无法在我的帖子上给出任何答案) 然后我测试了我在this Stack Overflow Page中遇到的两个选项:

第一个选项(由Ahmad Mageed提供)我已经测试过工作得很好。 可读性很容易理解(因为我对asp.net'技巧'还很新鲜)

然后按照答案 annakata是一个非常好的方法,你的方式 实际上不必重定向以实现结果 - 查询字符串被修改

在玩完游戏后,我已经决定遵循annakata的方法 并创建一个使用重定向选项的辅助方法 修改后的QueryString参数&值。

public void QuerStrModify(string CurrQS_ParamName, string NewQs_paramName, string NewPar_Value, bool redirectWithNewQuerySettings = 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);

    // remove 
    this.Request.QueryString.Remove(CurrQS_ParamName);

    // modify 
    this.Request.QueryString.Set(NewQs_paramName, NewPar_Value);

    // make collection readonly again 
    isReadOnly.SetValue(this.Request.QueryString, true, null);
    string FullUrl = Request.Url.AbsolutePath;
    if (redirectWithNewQuerySettings)
    {
        Response.Redirect(string.Join("?", FullUrl, this.Request.QueryString));
    }

}

我觉得对asp.net developmet经验相当少的人非常有帮助 所以我发布它作为我正确答案的版本,就像我看到的那样。 我希望它能帮助那些寻求相同解决方案的其他人。

随意进一步改进它,因为我提到我不是一个被证明有天赋的人.. ..

答案 1 :(得分:0)

您可以使用HttpModule

public class SimpleRewriter : System.Web.IHttpModule
{

    HttpApplication _application = null;

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new System.EventHandler(context_BeginRequest);
        _application = context;
    }

    public void Dispose()
    {
    }

    private void context_BeginRequest(object sender, System.EventArgs e)
    {
        string requesturl =
            _application.Context.Request.Path.Substring(0,
                _application.Context.Request.Path.LastIndexOf("//")
            );

        string[] parameters = requesturl.Split(new char[] { '/' });

        if (parameters.Length > 1)
        {
            string firstname = parameters[1];
            string lastname = parameters[2];


            //Here you can modify your parameters or your url

            _application.Context.RewritePath("~/unfriendly.aspx?firstname=" +
                firstname + "&lastname=" + lastname);

        }
    }
}

链接:http://msdn.microsoft.com/en-us/library/ms972974.aspx

注册:

<configuration>
  <system.web>
    <httpModules>
      <add name="SimpleRewriter" type="SimpleRewriter"/>
     </httpModules>
  </system.web>
</configuration>

链接:http://msdn.microsoft.com/en-us/library/ms227673%28v=vs.100%29.aspx

答案 2 :(得分:0)

这里的问题是“真相来源”问题。由NameValueCollection公开的HttpRequest.QueryString公开查询字符串,不应修改,因为查询字符串由调用者提供。如果应用程序具有UserName查询字符串参数但可能需要更改(例如用于测试),请将其包装在可以根据需要将其形成备用源的方法中。例如:

// A very simple container
public static class SystemInfo
{
    // This would be an instance of QueryStringUserInfo
    // by default but could be changed for testing.
    public IUserInfo UserInfo
    {
        get;
        private set;
    }
}

// An interface that contains the user operations 
public interface IUserInfo
{
    string UserName { get; }
}

// Get the user name from the query string. This would
// be the default.
public class QueryStringUserInfo: IUserInfo
{
    public string UserName
    {
        get
        {
            return Request.QueryString["UserName"].ToString();
        }
    }
}

// Get the user name from the query string. This would
// be the default.
public class TestUserInfo: IUserInfo
{
    public string UserName
    {
        get
        {
            return "Foo";
        }
    }
}

因此,不是直接为用户名(或任何一条信息)调用QueryString,而是在SystemInfo上调用该属性(可怕的名称,但你明白了)。它允许更改设置的来源,例如,如果代码在网页外部使用或进行测试。