简体   繁体   English

ASP.NET Core 和 C# 中的虚拟目录路径

[英]Virtual Directory Path in ASP.NET Core and C#

I've created a virtual directory in IIS, and I have a file that I want to get - like example.pdf .我在 IIS 中创建了一个虚拟目录,并且我有一个我想要获取的文件 - 例如example.pdf How do I get the virtual directory path and read the file in bytes?如何获取虚拟目录路径并以字节为单位读取文件? server.MapPath doesn't work in ASP.NET Core, and I didn't save the file in the wwwroot , I saved it to a folder Uploads outside the project, so I can't combine it with ContentRootPath or WebRootPath . server.MapPath在 ASP.NET Core 中不起作用,我没有将文件保存在wwwroot中,我将其保存到项目外的Uploads文件夹中,因此无法将其与ContentRootPathWebRootPath结合使用。

Can someone help me with this?有人可以帮我弄这个吗?

Currently my code is like this:目前我的代码是这样的:

var path = "D:/FolderOutsideProjects/Uploads/filename.pdf";
var bytes = await System.IO.File.ReadAllBytesAsync(path);
return File(bytes, contentType, Path.GetFileName(path));

Thanks谢谢

There is no native way for ASP.NET Core to resolve the Virtual Directory path. ASP.NET Core 没有本地方法来解析虚拟目录路径。 You could potentially read the IIS configuration file, but that seems very complicated.您可能会阅读 IIS 配置文件,但这看起来非常复杂。

The easy solution will just be adding your directory path to appsettings.json instead.简单的解决方案只是将您的目录路径添加到 appsettings.json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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