0

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

http://localhost/learnmore.aspx

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

string url=Request.RawUrl;

在这段代码之后,我进入/learnmore.aspx了 url 变量,但我想要完整的 URLhttp://localhost/learnmore.aspx

我怎样才能做到这一点?

4

4 回答 4

3
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

编辑:删除查询字符串项:(从Get url without querystring中找到)

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("?"));
于 2012-04-20T09:54:12.470 回答
1

您可以通过这种方式获取主机和方案:

Request.Url.GetLeftPart(UriPartial.Authority)

有了这个,你会得到:

http://localhost/

然后你可以附加 RawUrl

于 2012-04-20T10:13:51.193 回答
0

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

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + 
    Request.ApplicationPath.TrimEnd('/') + "/";
于 2012-04-20T09:53:46.240 回答
0

Request.Url.AbsoluteUri会成功的。

于 2012-04-20T09:54:17.350 回答