简体   繁体   English

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

[英]Unable to load static file once it has been published to Azure, using ASP.NET Core 2.1

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). 使用ASP.NET Core 2.1 ,我试图在本地机器上(通过Visual Studio 2017)在网站发布到托管环境(Azure Web Apps)之后加载相同的静态文件。

Consider the directory hierarchy in which the static file ( file.xml ) to be served resides inside the web root: 考虑目录层次结构,要提供服务的静态文件( file.xml )位于Web根目录内:

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

DirectoryNotFoundException: Could not find a part of the path 'D:\\home\\site\\wwwroot\\wwwroot\\xml\\file.xml' DirectoryNotFoundException:找不到路径'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. 无论我做什么,似乎都无法在文件发布到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. 注意:在ASP.NET MVC 5 ,我曾经能够使用Server.MapPath()函数加载静态文件,该文件在本地和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. 但是自从升级到ASP.NET Core 2.1以来,我不知道执行此操作的正确方法,当托管在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 这些是Microsoft文档,我已将其用作参考: https : //docs.microsoft.com/zh-cn/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. 如果将文件夹拖放到Solution Explorer ,则引用不会自动添加到.csproj文件中。

You can check your .csproj file by right clicking on the project and selecting Edit {Project Name}.csproj . 您可以通过右键单击项目并选择Edit {Project Name}.csproj来检查.csproj文件。 For any extra folders you've added to the wwwroot directory, you should see something like: 对于您添加到wwwroot目录中的所有其他文件夹,您应该看到类似以下内容:

<ItemGroup>
    <Folder Include="wwwroot\myfolder\" />
</ItemGroup>

If you don't see that, then it won't be published to Azure. 如果您看不到它,那么它将不会发布到Azure。

NOTE: If you see <None ... , then that means that the folder has been set to be excluded from publishing. 注意:如果您看到<None ... ,则意味着该文件夹已被设置为排除发布。

<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. 我只是删除了拖放到解决方案资源管理器中的文件夹,而是右键单击wwwroot以这种方式创建该文件夹。

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. 如果您在本地运行dotnet publish ,您可能会看到相同的行为,而该文件可能会从publish文件夹中丢失。

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 至于为什么它不被发布,我不是那部分的专家,但是这个答案可能会有所帮助: ASP.NET Core:在发布时排除或包含文件

For those coming across this question but for .js files (or similar), make sure your environment is set correctly in your View . 对于那些遇到此问题但对于.js文件(或类似文件)的人,请确保在View正确设置了environment

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 . 所有工作都完成到site.js 不是 site.min.js Publishing will use a different environment than what you're developing in and that makes an impact on your results. 发布将使用与开发environment不同的environment ,这会对您的结果产生影响。

Try: 尝试:

XmlDocument doc = new XmlDocument();
doc.Load(_env.ContentRootPath + "\\xml\\file.xml", FileMode.Open);

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

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