截断查询字符串和放大器;返回清洁网址C#ASP.net [英] Truncating Query String & Returning Clean URL C# ASP.net
问题描述
我想借原始URL,截断查询字符串参数,并返回URL的清理版本。我想它在整个应用程序出现,因此,通过执行在Global.asax将是理想的。另外,我觉得301重定向将是顺序。
I would like to take the original URL, truncate the query string parameters, and return a cleaned up version of the URL. I would like it to occur across the whole application, so performing through the global.asax would be ideal. Also, I think a 301 redirect would be in order as well.
即
在:www.website.com/default.aspx?utm_source=twitter&utm_medium=social-media
in: www.website.com/default.aspx?utm_source=twitter&utm_medium=social-media
出:www.website.com/default.aspx
out: www.website.com/default.aspx
什么是实现这一目标的最佳途径?
What would be the best way to achieve this?
推荐答案
的System.Uri这里是你的朋友。这上面有很多有用的实用程序,但是你想要的是GetLeftPart:
System.Uri is your friend here. This has many helpful utilities on it, but the one you want is GetLeftPart:
string url = "http://www.website.com/default.aspx?utm_source=twitter&utm_medium=social-media";
Uri uri = new Uri(url);
Console.WriteLine(uri.GetLeftPart(UriPartial.Path));
这给出了输出: http://www.website.com/default.aspx
[Uri类确实需要的协议,HTTP://,被指定]
[The Uri class does require the protocol, http://, to be specified]
GetLeftPart basicallys说:得到URI的最多的我指定的部分的左部。这可能是方案(只是http://位),管理委员会(www.website.com部分),路径(/default.aspx)或查询(查询字符串)
GetLeftPart basicallys says "get the left part of the uri up to the part I specify". This can be Scheme (just the http:// bit), Authority (the www.website.com part), Path (the /default.aspx) or Query (the querystring).
假设你是一个aspx页面上,你可以再使用的Response.Redirect(NEWURL)重定向来电。
Assuming you are on an aspx web page, you can then use Response.Redirect(newUrl) to redirect the caller.
希望帮助
这篇关于截断查询字符串和放大器;返回清洁网址C#ASP.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!