ASP.NET(C#) WebApi中如何访问项目引用内的子文件夹?
Hey there, let's work through this path issue step by step—it's a common gotcha with referenced projects and ASP.NET/IIS environments!
First, let's understand why your current paths aren't working
The problem boils down to two key things:
- Referenced project file copying: When you build your main project, files from referenced projects don't always retain their original directory structure by default. They might get dumped directly into the main project's output root instead of
services/actions/pdfs. - Unreliable path sources:
Assembly.GetExecutingAssembly().Locationpoints to the shadow-copied assembly path in IIS/debug environments (a temporary cache folder), not your actual output directory.Environment.CurrentDirectorycan be IIS's working directory, not your project's folder.
Here's how to fix it
1. Use a stable base directory
For most .NET/ASP.NET projects, AppContext.BaseDirectory is the most reliable way to get your application's root output directory. Try this path instead:
// Get the root directory of your running application var appRoot = AppContext.BaseDirectory; // Combine with your pdf folder path (adjust based on where the files actually end up) string pdfPath = Path.Combine(appRoot, @"pdfs\your-file-name.pdf");
If you're working with an ASP.NET Core project, even better—inject IWebHostEnvironment to get the project's content root (the folder where your .csproj lives):
private readonly IWebHostEnvironment _webHostEnv; // Inject via constructor (dependency injection) public ActionDB(IWebHostEnvironment webHostEnv) { _webHostEnv = webHostEnv; } public void YourConversionMethod() { // Path directly to your referenced project's folder structure string pdfPath = Path.Combine(_webHostEnv.ContentRootPath, @"services/actions/pdfs/your-file-name.pdf"); }
2. Ensure your referenced project preserves directory structure
Go to your referenced project's .csproj file and verify the content entry for your PDFs looks like this (this ensures the folder structure is kept when copying to output):
<Content Include="services\actions\pdfs\*.pdf"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToPublishDirectory>Always</CopyToPublishDirectory> <PreserveNewest>True</PreserveNewest> </Content>
If you just set the file properties via Visual Studio, sometimes the directory structure isn't preserved—manually checking the .csproj avoids that.
3. Debug the actual path to troubleshoot
Add a debug line to print the full path you're generating:
Debug.WriteLine($"Generated path: {pdfPath}");
Then go check if that path exists on your system. This will tell you if the issue is:
- The path you're building is wrong, or
- The PDF files aren't being copied to that location at all (in which case you need to fix the copy settings in the referenced project)
Quick recap
- Ditch
Assembly.LocationandEnvironment.CurrentDirectory—they're not reliable for web/IIS environments - Use
AppContext.BaseDirectory(for non-ASP.NET Core) orIWebHostEnvironment(for ASP.NET Core) to get a stable base path - Make sure your referenced project's .csproj preserves the directory structure when copying content files
内容的提问来源于stack exchange,提问作者CBD

