C# 如何从 url 中删除查询字符串参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1951038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove query string parameters from the url?
提问by Xaisoft
Assume I have the link http://www.somesite.com/file.aspx?a=1&b=2
假设我有链接http://www.somesite.com/file.aspx?a=1&b=2
And now I want to remove all the parameters, so it becomes:
现在我想删除所有参数,所以它变成:
http://www.somesite.com/file.aspx
http://www.somesite.com/file.aspx
Or I may want to remove only 1 of the parameters such as
或者我可能只想删除其中 1 个参数,例如
http://www.somesite.com/file.aspx?b=2
http://www.somesite.com/file.aspx?b=2
Is there a way to do the above in C#? What is happening is that I am coming from another page with a parameter called edit in the url, but when the page does a post back, the edit parameter is still there, so it still thinks it is in edit mode. Example:
有没有办法在 C# 中执行上述操作?发生的事情是我来自另一个页面,在 url 中有一个名为 edit 的参数,但是当页面回发时,edit 参数仍然存在,所以它仍然认为它处于编辑模式。例子:
User A goes to page one.aspx and clicks on an edit link. They are taken to two.aspx?edit=true. During page load, it sees the the query string parameter edit is not null and it puts the contents in edit mode. Once the user is done editing, the page is refreshed, but the url is still two.aspx?edit=true and keeps the contents in edit mode, when in fact it should be two.aspx
用户 A 转到页面 one.aspx 并单击编辑链接。它们被带到 two.aspx?edit=true。在页面加载期间,它看到查询字符串参数编辑不为空,并将内容置于编辑模式。一旦用户完成编辑,页面被刷新,但 url 仍然是 two.aspx?edit=true 并将内容保持在编辑模式,而实际上它应该是 two.aspx
采纳答案by this. __curious_geek
Request.Querystring is read-only collection- You cannot modify that.
Request.Querystring 是只读集合- 您不能修改它。
If you need to remove or change the param in querystring only way out is to trigger a new GET request with updated querystring - This means you will have to do Response.Redirect with updated URL. This will cause you lose the viewstate of the current page.
如果您需要删除或更改查询字符串中的参数,唯一的出路是使用更新的查询字符串触发新的 GET 请求 - 这意味着您必须使用更新的 URL 执行 Response.Redirect。这将导致您丢失当前页面的视图状态。
回答by Jeff Beck
When you are done with the edit you are doing a post back so just define the action to post to two.aspx instead of just posting back to itself that way it will drop off the get parameters.
完成编辑后,您将进行回发,因此只需定义要发送到 two.aspx 的操作,而不是仅以这种方式回发到自身,这样它就会删除 get 参数。
回答by RickNZ
How about checking Page.IsPostBack to see if the current request is a postback or not?
如何检查 Page.IsPostBack 以查看当前请求是否为回发?
回答by jannagy02
Use the PostBackUrl property, for example:
使用 PostBackUrl 属性,例如:
<asp:Button ID="DoneEditingButton" runat="server" Text="Done editing" PostBackUrl="~/two.aspx" />
回答by Marek Manduch
if you have only string, you can use:
如果您只有字符串,则可以使用:
strinULR.Split('?').First();
or
或者
strinULR.Split('?').FirstOrDefault();
回答by alternatefaraz
Late but you can do this to remove query string from URL without another GET Request. http://www.codeproject.com/Tips/177679/Removing-Deleting-Querystring-in-ASP-NET
晚了,但您可以这样做以从 URL 中删除查询字符串,而无需另一个 GET 请求。 http://www.codeproject.com/Tips/177679/Removing-Deleting-Querystring-in-ASP-NET
回答by Sonosp
Try something like this.
尝试这样的事情。
if (url.Contains("?"))
url = url.Substring(0, url.IndexOf("?"));
In this example, I'm checking if the url even contains a query string, and if it does, subtracting getting the "left part" of the string prior to the ?.
在此示例中,我正在检查 url 是否甚至包含查询字符串,如果包含,则减去在 ? 之前获取字符串的“左部分”。