清除查询字符串 PostRequestHandlerExecute

clear query string PostRequestHandlerExecute

我需要清除 queryString 所以我实现了一个 httpmodule

 public void Init(HttpApplication context)
            {
             context.PostRequestHandlerExecute += newEventHandler(OnEndRequest);

    }

这是方法:

 public void OnEndRequest(Object sender, EventArgs e)
        {
            HttpApplication context = (HttpApplication)sender;


            if (HttpContext.Current.Request.QueryString != null && HttpContext.Current.Request.QueryString.Count != 0)
            { HttpContext.Current.Request.QueryString.Clear(); }
    }

但它给出了一个例外:

System.NotSupportedException was unhandled by user code
  HResult=-2146233067
  Message=Collection is read-only.

目的是隐藏地址栏显示的参数,为了让用户无法更改参数的值,我不能使用post方法,因为,它是整个应用程序的问题,所以我不能返回所有页面并更改所有代码。 了解;我希望处理请求 MyPage.aspx?Param1=1 并将响应返回给客户端,但地址栏必须显示 MyPage.aspx 而不是 MyPage.aspx?Param1=1

清除查询字符串的唯一方法是通过响应重定向,例如

Response.Redirect(Request.RawUrl.Replace(Request.Url.Query, ""));

the purpuse is to hide the parameters shown in the adress bar, in order to make it impossible to the user to change the values of the parameters

HTTP 不是这样工作的。 HTTP 应用程序对客户端发送的请求进行操作,因此根据定义,客户端可以查看和更改他们想要发送的所有变量。

I want that the request MyPage.aspx?Param1=1 be processed and the response returned to the client but the addres bar must show MyPage.aspx instead of MyPage.aspx?Param1=1

您不能在当前请求期间以任何有意义的方式更改 "address bar"(显示当前请求 URL)。即使您可以,例如使用 JavaScript(导致根本不发送值)或通过发出重定向在服务器端(为时已晚,值已经发送),这仍然没有抓住要点:用户可以查看和更改他们发送的所有内容。

如果这个 Param1=1 实际上是服务器生成的秘密,则对其进行加密。或者,如果它是一个值,他们可以更改为他们无权访问的对象的标识符,然后在处理这些值之前检查服务器端的权限。

获得此功能(清除请求参数)的最佳且简单的方法是 使用客户端方法:

此代码将更新地址栏中显示的 url 而无需刷新页面(对于使用 html5 的浏览器)

  var urlElts = document.documentURI.split('?');
                var newUrl = urlElts[0];
                window.history.replaceState("object or string", "Title", newUrl);