ASP.NET: HttpRequest.Url truncates trailing '.

2019-09-01 09:12发布

问题:

If the URL that arrives to ASP.NET application contains trailing full stops - '.', they are truncated from the Url property in HttpRequest.

For example if the URL is "http://server/folder.../", the following call:

HttpContext.Current.Request.Url.PathAndQuery;

returns "/folder/" instead of "/folder.../".

Tried this solution, but it helps only if the Uri is constructed after the suggested code executes, while HttpRequest is probably constructed before any code in ASP.NET web application is executed.

Any ideas how to preserve trailing '.' in HttpRequest.Url?

回答1:

You can add the relaxedUrlToFileSystemMapping to your web.config inside <system.web> section.

  <httpRuntime relaxedUrlToFileSystemMapping="true" />

This will preserve the dots in the url.

But for some reason Url.PathAndQuery wont contain the dots, while RawUrl contains them.

HttpContext.Current.Request.Request.RawUrl;

Keep in mind that there are probably some security implications when enabling relaxedUrlToFileSystemMapping.