I would like to clear my querystring after using it on page load c#

*爱你&永不变心* 提交于 2019-12-25 09:00:04

问题


Before I ask this question, I'm not even sure what I'm using is a query string (I'm so clueless on this, what I have is the result of some other confusing StackOverFlow research). It is a parameter I'm passing from my SSRS report viewer to my app via a hyperlink expression. It works and everything is grand except for I'd like to clear it from the url right afterwards.

http://10.155.54.101/Update?CurrencyId=67

And I am getting the parameter with this logic on page load.

if (Request.Params["CurrencyId"] != null)
    int CurrencyId = int.parse(Request.Params["CurrencyId"]);

I am successfully capturing that information and populating asp.net controls with it but I want to clear it from the address bar now as it lingers after submitting the update (postback?).

Through another Stack Overflow Answer: Clear QueryString on PostBack , I've attempted to clear the querystring through the following code.

Request.Params.Clear();

But I get a collection is read-only error, which is addressed in the stack overflow question above. So I try to use System.Reflection to change the read only property of the collection with the following code.

PropertyInfo Isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
                Isreadonly.SetValue(Request.Params, false, null);
                Request.Params.Clear();

I don't get the error but nothing is removed, so I might not be referencing the querystring properly because of however the heck Request.Params works.

Can someone nudge me in the right direction with this? I'm so sorry I'm clueless as heck on this.


回答1:


You can't just change the URL in the address bar of a browser. You could redirect the browser to the URL without the query string, but seeing how you are using the value to populate controls on the page being render that would mean you would need to still need to have that value.

When you say "it lingers after submitting the update", do you mean the user chooses the currency and is redirected to the the page with this query string? If so could you change this action to a POST instead of a GET? Then you could put the currencyId in the body of the POST. If you can't switch it to a POST, then there are a few ideas I listed below.

If you are using session, you could store the currencyId in the user's session. But that would only make sense if you needed to use this value on other requests; as using session is a big decision and if you can keep your website stateless you should.

With that being said, there are two viable options to keep your site stateless. If you need this value on future requests, you can store it in a cookie. If you only need it on this request, you could have the page do post to the URL without the query string but with the value in the POST body.



来源:https://stackoverflow.com/questions/42401734/i-would-like-to-clear-my-querystring-after-using-it-on-page-load-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!