【问题标题】:Is there a way to clear query string parameters when posting back?回发时有没有办法清除查询字符串参数?
【发布时间】:2009-05-12 17:26:40
【问题描述】:

我有一个有时会与一些查询字符串参数链接的表单。问题是当我回发表单时,查询字符串参数仍然存在。我设置它的方式并不是真正的问题,但我只是不喜欢它在那里,如果您需要按特定顺序检查输入,它可能会出现问题。

有没有办法以一种简单、干净的方式清除该查询字符串参数?我知道我可以更改按钮上的 PostBackURL,但这似乎不太有效。

【问题讨论】:

    标签: asp.net postback query-string postbackurl


    【解决方案1】:

    不,我还没有看到没有重定向的方法来清除它。

    【讨论】:

    • 是否有一种干净的方法可以对您当前所在的页面执行 Response.Redirect()?
    • 尝试:Response.Redirect(Request.CurrentExecutionFilePath())
    • 我知道这真的很旧,但 CurrentExecutionFilePath 是一个属性,所以你不应该在最后加上括号。也许考虑将您的评论添加到您的答案中以使其更完整?
    【解决方案2】:

    我想你已经回答了你自己的问题。使用 PostBackURL 属性。

    <asp:Button PostBackUrl='<%# Request.ServerVariables["URL"] %>' runat="server" Text="Submit" />
    

    或者类似的东西

    foreach (Control ctrl in Page.Controls)
    {
        if (ctrl is Button)
        {
            ((Button)ctrl).PostBackUrl = Request.ServerVariables["URL"];
        }
    }
    

    【讨论】:

      【解决方案3】:

      把它放在页面底部?

      <script language="javascript" type="text/javascript">
          document.forms[0].action = window.location.pathname;
      </script>
      

      【讨论】:

      • 或来自代码隐藏:form1.Action = Request.Url.GetLeftPart(UriPartial.Path);
      【解决方案4】:

      我遇到了同样的问题。

      我使用以下代码块解决了它:

      private void ClearQueryString()
      {
          PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
          isreadonly.SetValue(this.Request.QueryString, false, null);
          this.Request.QueryString.Remove("Action");
          this.Request.QueryString.Remove("param1");
          this.Request.QueryString.Remove("paramN");
      }
      

      【讨论】:

        【解决方案5】:

        在某些情况下,您可以使用Server.Transfer() 方法,该方法有一个重载,允许您指定是否应保留查询字符串和表单数据。

        【讨论】:

          【解决方案6】:

          我认为由于某种原因您不能依赖 Page.IsPostBack?

          如果您正在做的是服务器端,那么在您的方法(Page_Load、OnInit 等)中添加对 IsPostBack 的检查很简单,并且仅在不是回发(即初始请求)时才处理查询字符串.

          【讨论】:

            【解决方案7】:

            我刚刚遇到了同样的问题,在网上搜索了一番后,我发现了这个 sn-p:

            public void ClearQueryStrings()
            {
                string clientCommand = string.Format(
                  "document.forms[\"{0}\"].action = \"{1}\";",
                     this.Form.Name, Request.Url.Segments[Request.Url.Segments.Length - 1]);
            
                ClientScript.RegisterStartupScript(this.GetType(), "qr", clientCommand, true);
            }
            

            在处理完原始查询字符串参数后,我在 Page_Load 中调用 ClearQueryStrings()。当页面回传时,参数就没有了。

            Original article is here.

            【讨论】:

              【解决方案8】:
              using System.Collections;
              using System.Reflection;
              

              使用以下代码清除查询字符串参数。

              // To clear Query string value
              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("YourQueryStringParameter");
              

              【讨论】:

                【解决方案9】:

                这个方法不是你所说的重定向,但它肯定会删除查询字符串。 只需将此行添加到 JavaScript 代码的末尾即可:

                window.location.search = "";
                

                或者

                将此添加到 HTML 页面的末尾:

                <script type="text/javascript">
                    window.location.search = "";
                </script>
                

                根据插入代码的位置,擦除查询字符串的时间会有所不同。

                【讨论】:

                  【解决方案10】:
                  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("status");
                  

                  【讨论】:

                  • 这不是大多数其他答案已经暗示的吗?
                  【解决方案11】:

                  你可以试试Response.Redirect(Request.CurrentExecutionFilePath()); 它对我有用。

                  【讨论】:

                    【解决方案12】:

                    当 javascript 代码被执行时,它会清理 url

                    <script language="javascript" type="text/javascript">
                       var urlWithoutParams = location.protocol + "//" + location.host + location.pathname;
                    
                        function clearCurrentURL(){
                            window.history.replaceState({}, document.title, urlWithoutParams);
                        }
                    </script>
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2013-08-07
                      • 1970-01-01
                      相关资源
                      最近更新 更多