代码之家  ›  专栏  ›  技术社区  ›  LoneXcoder

更改QueryString参数/值

  •  0
  • LoneXcoder  · 技术社区  · 12 年前

    我想更好地理解为url设置新参数的问题 并通过

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

    因此,如果我有一个URL:“http://www.myWebsite.aspx?UserName=Alice”

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

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

    但是如果我想更改值,例如使UserName=“Ralf”

    • 重新编辑

    当按下按钮时, 有一个参数“state”保存了对按钮被按下的引用 state的值为=“none” 现在我想把它设置为imgbutton1。

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

    我只是为了测试/引用而硬编码

    这样我就可以知道我正处于特定事件的执行者所要求的事件阶段 按钮1

    事件由触发时 img_button2

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

    3 回复  |  直到 12 年前
        1
  •  3
  •   Community Egal    4 年前

    在我做了研究之后(我无法在帖子上标记任何善意的答案) 然后我测试了在中遇到的两个选项 this Stack Overflow Page 以下为:

    第一个选项(由Ahmad Mageed给出)我已经测试过可以正常工作。 可读性很容易理解(因为我对asp.net的“技巧”还很陌生)

    然后回答 annakata,这是一个显著改进的方法 实际上不必重定向即可获得结果-查询字符串已修改

    玩了一番之后,我决定跟着玩 annakata s方法 并制作一个同时使用重拨选项的辅助方法 具有修改的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开发经验少得多的人很有帮助 所以我把它作为我的正确答案发布,正如我所看到的。 我希望它能帮助其他寻求同样解决方案的人。

    我说过,我不是一个公认的天才。。然而

        2
  •  0
  •   Aghilas Yakoub    12 年前

    您可以使用 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

        3
  •  0
  •   akton    12 年前

    这里的问题是“真相的来源”问题。这个 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上的属性(糟糕的名称,但你明白了)。它允许更改设置的来源,例如如果代码在网页外部使用或用于测试。