【问题标题】:How to get complete URL of browser in UrlRewriting using C#如何使用 C# 在 UrlRewriting 中获取浏览器的完整 URL
【发布时间】:2012-04-20 09:46:16
【问题描述】:

我使用过 URL 重写。我有一个问题是我有像

这样的 URL
http://localhost/learnmore.aspx

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

string url=Request.RawUrl;

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

我该怎么做?

【问题讨论】:

  • 不明白这与 URL 重写有什么关系,你只是不想完整的 URL 吗? Request.Url.AbsoluteUri ?
  • 是的,但是如果我使用 Request.Url.AbsoluteUri 然后我得到localhost/Default.aspx?pageid=25 所以它对我没有用,这是由于 URL 重写

标签: c# url


【解决方案1】:
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("?"));

【讨论】:

【解决方案2】:

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

Request.Url.GetLeftPart(UriPartial.Authority)

有了这个你会得到:

http://localhost/

然后你可以附加 RawUrl

【讨论】:

    【解决方案3】:

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

    string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + 
        Request.ApplicationPath.TrimEnd('/') + "/";
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-10
        • 1970-01-01
        • 1970-01-01
        • 2021-09-13
        • 1970-01-01
        • 2011-07-01
        • 1970-01-01
        相关资源
        最近更新 更多