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

Ras*_*ber 7 asp.net url url-encoding rawurl

在ASP.NET中,有没有办法获得真正的原始URL?

例如,如果用户浏览到" http://example.com/mypage.aspx/%2F ",我希望能够获得" http://example.com/mypage.aspx/%2F "比" http://example.com/mypage.aspx// ".

我当然喜欢干净的方式,但我可以使用反射或访问模糊属性的hacky方法.

目前,我尝试在Authorization-header(可行)中使用uri,但我不能依赖于那里总是存在的.

编辑:

我真正想要做的是能够区分" http://example.com/mypage.aspx/%2F "和" http://example.com/mypage.aspx/%2F%2F ".

看起来ASP.NET首先将"%2F%2F"转换为"//",然后将斜杠转换为单个斜杠.

所以只是重新编码它是行不通的.

Wil*_*oss 6

我无法测试这个,因为它只适用于IIS而不是Visual Studio中的ASP.NET Development Server,但请尝试:

Request.ServerVariables ["HTTP_URL"]


Ras*_*ber 6

以下代码适用于我:

IServiceProvider serviceProvider = (IServiceProvider)HttpContext.Current;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)serviceProvider.GetService(typeof(HttpWorkerRequest));
string realUrl = workerRequest.GetServerVariable("HTTP_URL");
Run Code Online (Sandbox Code Playgroud)

请注意,这只适用于在IIS上运行而不是在fx ASP.NET Development Server下运行!

感谢Lucero在另一个线程中的答案和Zhaph指向我的线程.