使用ASP.NET Core 2.1将静态文件发布到Azure后无法加载

问题描述:

使用 ASP.NET Core 2.1 ,我正在尝试将我的开发机器上本地加载相同的静态文件(通过Visual Studio 2017),并将网站发布到托管环境之后(Azure Web应用程序.)

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).

请考虑要在其中提供静态文件( file.xml )驻留在Web根目录中的目录层次结构:

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

这会将文件本地加载到我的开发机器上,但是,一旦将Web应用发布到Azure,我会收到以下错误消息:

This loads the file locally on my dev machine, however, once the web app is published to Azure, I get the following error:

DirectoryNotFoundException:找不到路径的一部分'D:\ home \ site \ wwwroot \ wwwroot \ xml \ file.xml'

DirectoryNotFoundException: Could not find a part of the path 'D:\home\site\wwwroot\wwwroot\xml\file.xml'

无论我尝试什么,在将文件发布到Azure之后,似乎都无法加载文件.

No matter what I try, I cannot seem to load the file after it has been published to Azure.

注意:在 ASP.NET MVC 5 中,我曾经能够使用 Server.MapPath()函数来加载静态文件,该文件在本地均可工作并在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"));

但是自从升级到 ASP.NET Core 2.1 以来,我不知道执行此操作的正确方法,当在Azure上托管时,该方法也将起作用.

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.

这些是Microsoft Docs,我已将其用作参考: https://docs.microsoft.com/zh-CN/aspnet/core/fundamentals/static-files?view=aspnetcore-2.1&tabs=aspnetcore2x

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

更新

如果其他任何人遇到相同的问题,这就是问题所在:

In case anyone else experiences the same issue, this is what the problem was:

如果将文件夹拖放到 Solution Explorer 中,则引用不会自动添加到 .csproj 文件中.

If you drag and drop a folder into Solution Explorer, the reference does not get automatically added to the .csproj file.

您可以通过右键单击项目并选择 Edit {Project Name} .csproj 来检查您的 .csproj 文件.对于您添加到 wwwroot 目录的所有其他文件夹,您应该会看到类似以下内容的文件:

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>

如果看不到,则不会发布到Azure.

If you don't see that, then it won't be published to Azure.

注意:如果看到< None ... ,则表明该文件夹已被设置为不发布.

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 Explorer中的文件夹,而是右键单击 wwwroot 来创建该文件夹.

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.

看来,您的问题是此文件未发布,并且与运行时发生的情况无关.

It appears that your issue is that this file is not getting published, and it is not an issue with what happens at runtime.

如果在本地运行 dotnet publish ,您可能会看到相同的行为,而该文件可能会从publish文件夹中丢失.

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