Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MapPath and AppDomainAppPath #184

Merged
merged 10 commits into from
Sep 8, 2022

Conversation

CZEMacLeod
Copy link
Contributor

Implement HttpRuntime.AppDomainAppPath, HttpServerUtility.MapPath, and HttpServerUtilityWrapper.MapPath

Provide support for MapPath
Paths are mapped relative the application root directory - available as HttpRuntime.AppDomainAppPath

This may not behave as expected, for example if a person uses MapPath to write uploaded files to disk and expects them to be served, due to the way AspNetCore uses ContentRootPath/WebRootPath.
In this case it might be better to implement your own helper function which can use something like

namespace System.Web.Extensions
{
    public static class MapPathExtension
    {
        public static string MapContentPath(this System.Web.HttpContext context, string? path)
        {
#if NET6_0_OR_GREATER
            var appPath = path is null ? VirtualPathUtility.GetDirectory(context.Request.Path) :
                VirtualPathUtility.Combine(
                VirtualPathUtility.GetDirectory(context.Request.Path) ?? "/"
                , path);
            var coreContext = (Microsoft.AspNetCore.Http.HttpContext)context;
            var rootPath = coreContext.RequestServices.GetRequiredService<IWebHostEnvironment>().WebRootPath;
            if (string.IsNullOrEmpty(appPath)) return rootPath;
            return System.IO.Path.Combine(rootPath, appPath[1..].Replace('/', System.IO.Path.DirectorySeparatorChar));
#else
            return context.Server.MapPath(path);
#endif
        }
    }
}

Addresses #183

@twsouthwick
Copy link
Member

@CZEMacLeod this builds on #179, right? If so, I'll hold off on reviewing until we get that in.

@CZEMacLeod
Copy link
Contributor Author

@twsouthwick Yes - It uses the VirtualPathUtility from #179 to handle the app relative and request relative paths and prevent 'exiting' up above the site directory.

@CZEMacLeod
Copy link
Contributor Author

@twsouthwick The tests I've added seem to be failing on Linux/Mac - I think because the expected results rely on the directory separator character but I'm not sure how to find the output, or what to do to mitigate this...

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.

Can you clean up the comments and such in the MapPath implementation to just have the direction we decided on?

@twsouthwick
Copy link
Member

Thanks @CZEMacLeod for the contribution! LGTM

@twsouthwick twsouthwick requested a review from Tratcher September 8, 2022 15:18
@twsouthwick twsouthwick merged commit bb35430 into dotnet:main Sep 8, 2022
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.

3 participants