隐藏

C#如何在URL重写中获取浏览器的完整URL

发布:2023/1/5 17:06:34作者:管理员 来源:本站 浏览次数:339

我使用了URL重写。 我有一个问题是我有类似的网址



http://localhost/learnmore.aspx


这是URL覆盖,所以我想要这个完整的URL,所以我已经这样编码。



string url=Request.RawUrl;


在此代码之后,我在url变量中得到了/learnmore.aspx,但是我想要完整的URL http://localhost/learnmore.aspx


我怎样才能做到这一点?



string url = HttpContext.Current.Request.Url.AbsoluteUri;

// http://localhost/learnmore.aspx


string path = HttpContext.Current.Request.Url.AbsolutePath;

// /localhost/learnmore.aspx


string host = HttpContext.Current.Request.Url.Host;

// localhost


编辑:删除查询字符串项:(从获取没有查询字符串的url中找到)



var uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);

string path = uri.GetLeftPart(UriPartial.Path);


要么



Uri url = new Uri("http://www.somesite.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");

string path = String.Format("{0}{1}{2}{3}", url.Scheme,

   Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);


要么



string url ="http://www.somesite.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";

string path = url.Substring(0, url.IndexOf("?"));



您可以通过以下方式获得主机和方案:



Request.Url.GetLeftPart(UriPartial.Authority)


有了这个你会得到:


   http://localhost/


然后可以附加RawUrl


Request.Url.AbsoluteUri可以解决问题。


您可以尝试获取基本网址:



string baseUrl = Request.Url.Scheme +"://" + Request.Url.Authority +

   Request.ApplicationPath.TrimEnd('/') +"/";