Skip to content

HostingEnvironment.MapPath doesn't work correctly #838

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

Closed
axefrog opened this issue Aug 16, 2015 · 6 comments
Closed

HostingEnvironment.MapPath doesn't work correctly #838

axefrog opened this issue Aug 16, 2015 · 6 comments

Comments

@axefrog
Copy link

axefrog commented Aug 16, 2015

I'm observing strange behaviour from the HostingEnvironment argument in the Configure method of my Startup class.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.Run(async (context) =>
    {
        var str = "Testing\r\n";
        str += $"env.MapPath(\"~{context.Request.Path.ToString()}\") ===> {env.MapPath("~" + context.Request.Path.ToString())}\r\n";
        str += $"env.MapPath(\"{context.Request.Path.ToString()}\") ===> {env.MapPath(context.Request.Path.ToString())}";
        await context.Response.WriteAsync(str);
    });
}

The output I'm getting for a request to the root of the website is as follows:

  1. env.MapPath("~/a/b/c"); becomes: "D:\Dropbox\Work\Projects\...etc....\wwwroot\~\a\b\c"
  2. env.MapPath("/a/b/c"); becomes: "\a\b\c"

In other words, if I don't include a tilde, the path is not "mapped" at all; it simply twiddles the slash direction. If I do include a tilde, the full mapping occurs, but the tilde is not dropped.

For now, the workaround is to just use env.WebRootPath.

@rubenprins
Copy link

What you're seeing is, I've been told, intentional: IHostingEnvironment.MapPath is not, and will not be a replacement for Server.MapPath. It merely has an unfortunate name leading existing developers to think it is.

I've had a rather heated discussion about this in aspnet/Hosting/issues/222, but the short of it is: IHostingEnvironment.MapPath basically does a Path.Combine(env.WebRootPath, yourPath), and that's it. It has no notion of ~ paths, true (mapped) virtual paths or paths outside of your application path if you're running child applications under IIS.

What you need call is env.MapPath("a/b/c"), so no leading / or ~/. Which doesn't do anything you didn't already do yourself by appending your path to env.WebRootPath; it only takes care of the OS-specific directory separator translation.

@axefrog
Copy link
Author

axefrog commented Aug 19, 2015

Hmm, sounds redundant. Why have this method at all instead of just using Path.Combine(env.WebRootPath, yourPath)?

@stijnherreman
Copy link

@axefrog For convenience, I assume.

@aspnet-hello
Copy link

This issue is being closed because it has not been updated in 3 months.

We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.

@davidalpert
Copy link

davidalpert commented Dec 31, 2017 via email

@davidalpert
Copy link

davidalpert commented Dec 31, 2017 via email

natemcmaster pushed a commit that referenced this issue Nov 20, 2018
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants