问题
Using ASP.NET Core 2.1
, I'm trying to load the same static file locally on my dev machine (through Visual Studio 2017) and after the website has been published to the hosting environment (Azure Web Apps).
Consider the directory hierarchy in which the static file (file.xml
) to be served resides inside the web root:
wwwroot
css
images
js
xml
file.xml
Here's my controller to load the file:
public class DevelopmentController : Controller
{
private readonly IHostingEnvironment _env;
public DevelopmentController(IHostingEnvironment env)
{
_env = env;
}
public async Task<string> Test()
{
string result = "";
// Test Code Here
XmlDocument doc = new XmlDocument();
doc.Load(Path.Combine(_env.WebRootPath, "xml", "file.xml"));
return result;
}
}
This loads the file locally on my dev machine, however, once the web app is published to Azure, I get the following error:
DirectoryNotFoundException: Could not find a part of the path 'D:\home\site\wwwroot\wwwroot\xml\file.xml'
No matter what I try, I cannot seem to load the file after it has been published to Azure.
NOTE: In ASP.NET MVC 5
, I used to be able to use the Server.MapPath()
function to load a static file, which worked both locally and in Azure. For example:
doc.Load(System.Web.HttpContext.Current.Server.MapPath("~/xml/file.xml"));
But since upgrading to ASP.NET Core 2.1
, I can't figure out the proper way to do this, that will also work when hosted on Azure.
These are the Microsoft Docs, I've used as reference: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.1&tabs=aspnetcore2x
UPDATE
In case anyone else experiences the same issue, this is what the problem was:
If you drag and drop a folder into Solution Explorer
, the reference does not get automatically added to the .csproj
file.
You can check your .csproj
file by right clicking on the project and selecting Edit {Project Name}.csproj
. For any extra folders you've added to the wwwroot
directory, you should see something like:
<ItemGroup>
<Folder Include="wwwroot\myfolder\" />
</ItemGroup>
If you don't see that, then it won't be published to Azure.
NOTE: If you see <None ...
, then that means that the folder has been set to be excluded from publishing.
<ItemGroup>
<None Include="wwwroot\myexcludedfolder\"
</ItemGroup>
SOLUTION
I just deleted the folder that I had drag and dropped into Solution Explorer, and instead right clicked wwwroot
to create the folder that way.
回答1:
It appears that your issue is that this file is not getting published, and it is not an issue with what happens at runtime.
You would probably see the same behavior if you ran dotnet publish
locally, with that file probably missing from the publish folder.
As for why it is not getting published, I'm not an expert of that part, but this answer might help: ASP.NET Core: Exclude or include files on publish
回答2:
For those coming across this question but for .js
files (or similar), make sure your environment
is set correctly in your View
.
Default example would be this:
<environment exclude="Development">
<script src="~/js/site.min.js" asp-append-version="true"></script>
</environment>
Where all work was done into site.js
NOT site.min.js
. Publishing will use a different environment
than what you're developing in and that makes an impact on your results.
回答3:
Try:
XmlDocument doc = new XmlDocument();
doc.Load(_env.ContentRootPath + "\\xml\\file.xml", FileMode.Open);
来源:https://stackoverflow.com/questions/52117825/unable-to-load-static-file-once-it-has-been-published-to-azure-using-asp-net-co