2016-10-24 331 views
0

我在IIS 7.5中有一个网站,并尝试将文件上载到位于另一个驱动器上的“文件”文件夹中。我添加了虚拟文件夹文件wwwroot文件夹。IIS中的IEnvironment.WebRootPath和虚拟目录

该虚拟文件夹的路径是d:\文件

我在控制器上载代码:

_env变量是IHostingEnvironment和构造注射。

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

它在本地工作,因为我的机器上有物理文件夹wwwroot \ files。不过,这并不在生产服务器上运行,并得到了以下错误:

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

但我已经拿到了另一个网站的ASPNET 4.6.1和我以前使用Server.Mappath上传的文件。它可以在该网站上运行,我可以成功上传文件。

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

enter image description here

根据这篇文章,它说和使用Server.Mappath是WebRootPath类似。 http://www.mikesdotnetting.com/article/302/server-mappath-equivalent-in-asp-net-core

但是,在我看来,WebRootPath只给了我们网站的根路径。它不接受参数来评估给定的URL,如Server.MapPath。

您能否告诉我如何上传生产服务器上的文件,而不是在我的.Net Core应用程序中硬编码物理路径?

更新1:

这是我迄今为止达到最佳的...我们可以访问或创建另一个虚拟URL指向这样其他位置。它可以通过http://mysite/Files/abc.jpg

参考访问:ASPNetCore StaticFiles

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

然而,这仅仅是只读的网址,我不能用“/Files/newfile.jpg”或Path.Combine上传到这个路径(_env .WebRootPath,“newfile.jpg”),因为它没有物理存在。

+0

WebRootPath不知道关于IIS vdirs,你必须通过IWebHostBuilder.UseWebRoot(mywebroot) – Tratcher

+0

直接配置它你知道如何解决这个问题吗? – Ruchan

+0

@Ruchan您可以直接使用“D:\ mySharedfiles \”代替IIS虚拟文件夹 – TTCG

回答

0

这里是你如何配置内容和Web根:

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(); 
    } 
} 

可能UseWebRoot()和UseContentRoot()是你在找什么。

+0

直接写入物理文件夹。请您详细说明它是如何帮助我解决问题的?据我所知,这是用来指向wwwroot文件夹(WebRoot)和Content Root文件夹。我如何使用WebRoot或ContentRoot指向D:\文件中的文件? 如果我使用ContentRoot指向那个路径,我所有的视图都没有找到......它在D:\ files文件夹中找到视图。 – TTCG

+0

看看这里:https://docs.asp.net/en/latest/fundamentals/static-files.html。这是描述如何使用静态文件的文章。在我看来,如果所有这些改进(可能性改变根 - UseWebRoot()和UseContentRoot())你仍然无法解决你的问题,你应该退后一步,想想如果你真的必须在“D :\文件”。也许将它们存储在数据库中 - SQL Server支持非常棒的“文件表”功能。也许试试这种方法。 –

+0

是的,我必须。这些文件与其他2个Web应用程序共享。我们可以通过在经典的.Net中使用Server.MapPath(xxx)轻松实现相同的功能。所以,我认为有可能在.Net Core中做同样的事情。显然,答案是否:(我必须在appSettings.json中硬编码路径。谢谢你的回复。 – TTCG