简体   繁体   中英

IEnvironment.WebRootPath and Virtual Directory in IIS

I have got a website in IIS 7.5 and trying to upload files into the Files folder which is located on another drive. I added virtual folder called files onto the wwwroot folder.

That virtual files folder path is D:\\files

My code for the Upload in Controller:

_env variable is IHostingEnvironment and injected in Constructor.

var filename = _env.WebRootPath + $@"\files\{model.JobID}.{model.FileExt}";
using (FileStream fs = System.IO.File.Create(filename))
{
    AttachFile.CopyTo(fs);
    fs.Flush();
}

It works in local because I got the physical folder wwwroot\\files in my machine. But it doesn't work on the Production server and got the following error:

An unhandled exception has occurred: Could not find a part of the path 'C:\inetpub\Mysite\wwwroot\files\111214.png'.

But I have got the another website in ASPNet 4.6.1 and I used Server.MapPath to upload the files. And it works in that website and I can upload the files successfully.

string SaveLocation = string.Format(@"{0}\{1}{2}", Server.MapPath("files"), JobID, FileExt);

在此处输入图片说明

According to this article, it says Server.MapPath and WebRootPath are similar. http://www.mikesdotnetting.com/article/302/server-mappath-equivalent-in-asp-net-core

However, it seems to me that WebRootPath only gives us the RootPath of the website. It doesn't accept the parameter to evaluate the given url like Server.MapPath.

Could you please advise me how can I upload the files on the Production Server, rather than hardcoding the physical path in my .Net Core application?

Update 1:

This is the best I reach so far... We can access or create another virtual URL to point to another location in this way. It can be access via http://mysite/Files/abc.jpg

Ref: ASPNetCore StaticFiles

app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new PhysicalFileProvider(@"D:\Files"),
        RequestPath = new PathString("/Files"),                
    });

However, it is just readonly url and I cannot upload to this path by using "/Files/newfile.jpg" or Path.Combine(_env.WebRootPath, "newfile.jpg") because it doesn't physically exist.

Here is how you can configure content and web roots:

public class Program
{
    public static void Main(string[] args)
    {
        var contentRoot = Directory.GetCurrentDirectory();

        var config = new ConfigurationBuilder()
           .SetBasePath(contentRoot)
           .AddJsonFile("hosting.json", optional: true)
           .Build();

        //WebHostBuilder is required to build the server. We are configurion all of the properties on it
        var hostBuilder = new WebHostBuilder()

            .UseKestrel()

             //Content root - in this example it will be our current directory
            .UseContentRoot(contentRoot)

            //Web root - by the default it's wwwroot but here is the place where you can change it
            //.UseWebRoot("wwwroot")

            //Startup
            .UseStartup<Startup>()

            .UseConfiguration(config);

        var host = hostBuilder.Build();
        //Let's start listening for requests
        host.Run();
    }
}

Probably UseWebRoot() and UseContentRoot() is what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM