Skip to content

Conversation

@benamada
Copy link
Contributor

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

PR Title
Add HostingEnvironment.MapPath

PR Description
This PR adds a MapPath method to HostingEnvironment. It essentially just calls the existing IMapPathUtility.MapPath() method that HttpServerUtility.MapPath calls. With the new HostingEnvironment.MapPath method, an HttpContext is not required. Before IMapPathUtility.MapPath() is called, there's 2 checks to match the input HostingEnvironment.MapPath accepts in .NET Framework.

  • HostingEnvironment.MapPath doesn't accept paths such as file.txt. In contrast, HttpServerUtility.MapPath does accept this. To match HostingEnvironment.MapPath in .NET Framework, the new code throws an exception.
  • HostingEnvironment.MapPath does accept paths that are UNC like, e.g. \\file or //file, however MapPathUtility.MapPath() throws an error. To avoid the error and conform with the behavior in .NET Framework (without modifying MapPathUtility.MapPath()), the inbound virtualPath is modified by collapsing multiple leading slashes to a single slash, e.g. /file.

Addresses #460

@benamada
Copy link
Contributor Author

@dotnet-policy-service agree

@twsouthwick
Copy link
Member

Looks good @benamada - can you add some tests?

@twsouthwick
Copy link
Member

We're getting ready for a 2.0 release that if you can get that in the next couple days, it'll probably make it in

…MapPath() output to include/exclude a trailing slash based on whether the input has a trailing slash.
…ccount for possible null input and to match the nullable HttpServerUtility.MapPath path parameter.
@benamada
Copy link
Contributor Author

benamada commented Jan 7, 2025

@twsouthwick I added a new HostingEnvironmentTests.cs file in the Microsoft.AspNetCore.SystemWebAdapters.Tests project.

While testing, I discovered there's a small glitch with the original MapPathUtility.MapPath() method and made a small update to it. In .NET Framework, if the input to Server.MapPath and HostingEnvironment.MapPath includes a trailing slash, e.g. /file/, both MapPath methods return a physical path with a trailing slash. MapPathUtility.MapPath() (this project) always removes trailing slashes. This is a problem if there's code such as MapPath("/path/") + "file.txt" which would incorrectly become /pathfile.txt as opposed to /path/file.txt. The update to MapPathUtility.MapPath ensures the return string includes/excludes a trailing slash based on whether the input has a trailing slash.

Glad to hear this update has a chance to make the 2.0 release. If there's anything else you'd like to see, just let me know.

Copy link
Member

@twsouthwick twsouthwick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Thanks for the contribution

@benamada
Copy link
Contributor Author

benamada commented Jan 8, 2025

@twsouthwick good point, I added new tests in HttpServerUtilityTests and HostingEnvironmentTests for ~/ and /.

I initially had these tests in yesterday but they were failing on Linux and macOS. Digging into it further today, the values returned from MapPath() were correct and it was the expected values that were not being put together correctly, e.g. /apps/test-map-path// with a double trailing slash. Updated the tests today to account for this and they are passing now on all platforms.

@twsouthwick twsouthwick merged commit 880657b into dotnet:main Jan 8, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants