ASP.NET: Get *real* raw URL

给你一囗甜甜゛ 提交于 2020-01-01 09:33:09

问题


In ASP.NET, is there any way to get the real raw URL?

For example, if a user browse to "http://example.com/mypage.aspx/%2F", I would like to be able to get "http://example.com/mypage.aspx/%2F" rather than "http://example.com/mypage.aspx//".

I would of course like a clean way to do it, but I can live with a hacky approach using reflection or accessing obscure properties.

At the moment, I try to use the uri in the Authorization-header (which works), but I cannot rely on that always being there.

EDIT:

What I really want to do is to be able to distinguish between "http://example.com/mypage.aspx/%2F" and "http://example.com/mypage.aspx/%2F%2F".

It looks like ASP.NET first converts "%2F%2F" into "//" and then converts the slashes into a single slash.

So just re-encoding it is not going to work.


回答1:


I wasn't able to test this because it only works in IIS and not the ASP.NET Development Server that is part of Visual Studio, but try:

Request.ServerVariables[ "HTTP_URL" ]




回答2:


The following code works for me:

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

Note that this only works when running on the IIS and not under f.x. ASP.NET Development Server!

Thanks to Lucero for the answer in another thread and Zhaph for pointing me to the thread.




回答3:


See also:

Get the exact url the user typed into the browser




回答4:


 Server.HtmlEncode(Request.RawUrl);

The raw URL is defined as the part of the URL following the domain information. In the URL string http://www.contoso.com/articles/recent.aspx, the raw URL is /articles/recent.aspx. The raw URL includes the query string, if present.

see also:link text




回答5:


I can't test here, but this might be what you need:

Request.Url.AbsoluteUri



回答6:


Request.RawUrl will return the application relative path(including querystring info) while Request.Url will return the complete path(including querystring info).

For more information, see "Making sense of ASP.NET paths".




回答7:


Well, you could just encode it back to the url-encoded version.




回答8:


Get the url from the request and urlencode only the query string part and then concatenate them



来源:https://stackoverflow.com/questions/781596/asp-net-get-real-raw-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!