代码之家  ›  专栏  ›  技术社区  ›  Rasmus Faber

ASP.NET:获取*真实*原始URL

  •  7
  • Rasmus Faber  · 技术社区  · 15 年前

    在ASP.NET中,是否有任何方法可以获取 真实的 原始URL?

    例如,如果用户浏览到“ http://example.com/mypage.aspx/%2F “,我希望能够” http://example.com/mypage.aspx/%2f “而不是” http://example.com/mypage.aspx// “。

    当然,我希望有一种干净的方法来实现它,但是我可以使用一种使用反射或访问模糊属性的黑客方法来生活。

    目前,我尝试在授权头中使用URI(这是有效的),但我不能总是依赖于它。

    编辑:

    我真正想做的是区分 http://example.com/mypage.aspx/%2f “和” http://example.com/mypage.aspx/%2F%2F “。

    似乎ASP.NET首先将“%2f%2f”转换为“//”,然后将斜杠转换为单个斜杠。

    所以重新编码是行不通的。

    8 回复  |  直到 9 年前
        1
  •  6
  •   William Gross    15 年前

    我无法对此进行测试,因为它只在IIS中工作,而不是在作为Visual Studio一部分的ASP.NET开发服务器中工作,请尝试:

    request.servervariables[“http_url”]

        2
  •  6
  •   Community CDub    7 年前

    以下代码适用于我:

    IServiceProvider serviceProvider = (IServiceProvider)HttpContext.Current;
    HttpWorkerRequest workerRequest = (HttpWorkerRequest)serviceProvider.GetService(typeof(HttpWorkerRequest));
    string realUrl = workerRequest.GetServerVariable("HTTP_URL");
    

    请注意,只有在IIS上运行,而不是在F.X.ASP.NET Development Server下运行时,这才有效!

    多亏了 Lucero 在另一条线索中的答案 Zhaph 指给我指线。

        4
  •  1
  •   mirezus    15 年前
     Server.HtmlEncode(Request.RawUrl);
    

    原始URL定义为域信息后面的URL部分。在URL字符串中 http://www.contoso.com/articles/recent.aspx ,原始URL是/articles/recent.aspx。原始URL包含查询字符串(如果存在)。

    参见: link text

        5
  •  1
  •   Scott Mitchell    11 年前

    Request.RawUrl 将返回应用程序相对路径(包括querystring信息),同时 Request.Url 将返回完整路径(包括querystring信息)。

    有关详细信息,请参阅“ Making sense of ASP.NET paths “。

        6
  •  0
  •   asgerhallas    15 年前

    好吧,你可以把它编码回URL编码的版本。

        7
  •  0
  •   Martin    15 年前

    我不能在这里测试,但这可能是您需要的:

    Request.Url.AbsoluteUri
    
        8
  •  0
  •   Naren    15 年前

    从请求中获取URL,并只对查询字符串部分进行urlencode,然后将它们连接起来。