如何在C#中从Request.UrlReferrer.AbsoluteUri中移除查询字符串部分

8

我希望能在C#中重定向之前从Request.UrlReferrer.AbsoluteUri中删除查询字符串部分。

例如,如果您有自己的

Request.UrlReferrer.AbsoluteUri = "http://localhost:8080/english/index_2011.aspx?logout=true"

现在我想要...
Response.Redirect(Request.UrlReferrer.AbsoluteUri) without QueryString part (?logout=true")

请使用C#。
3个回答

11

使用 Request.UrlReferrer.AbsoluteUri.ToString().Split('?')[0]

这个应该适合你的需求。


10
更清晰的方式是:
Request.UrlReferrer.GetLeftPart(UriPartial.Path)

我的意思是我希望得到路径之前的所有内容。它应该返回:

"http://localhost:8080/english/index_2011.aspx"

是的,但它怎么能只返回 /english/index_2011.aspx 呢? - JoshYates1980

6

Response.Redirect(Request.UrlReferrer.AbsoluteUri.Substring(0,Request.UrlReferrer.AbsoluteUri.IndexOf('?')));

EDIT

实际上,你可以使用以下代码:

Response.Redirect(Request.UrlReferrer.AbsolutePath);

请在MSDN上查看。


为什么我们要使用-1,这会从我的aspx扩展名中删除“x” - Manoj Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接