简体   繁体   English

ASP.NET Core 托管在 Windows 服务中 - 未提供静态文件(即 /wwwroot 的内容)

[英]ASP.NET Core hosted in a Windows Service - static files not being served (i.e. contents of /wwwroot)

I have an ASP.NET Core v3.1 application hosted in a Windows Service.我有一个托管在 Windows 服务中的 ASP.NET Core v3.1 应用程序。 When I run the service locally, everything works as expected.当我在本地运行服务时,一切都按预期工作。 However, when I publish it and deploy it out to a server the static files (ie the contents of the /wwwroot folder) are not being served, eg但是,当我发布它并将其部署到服务器时,不会提供静态文件(即 /wwwroot 文件夹的内容),例如

在此处输入图片说明

My service exists on my file system like so, and the wwwroot contains all the necessary files:我的服务像这样存在于我的文件系统中,wwwroot 包含所有必要的文件:

在此处输入图片说明

As per Microsoft documentation , I understand that the reason for this is because the working directory of the Windows service is the C:\\Windows\\system32 folder, which isn't a suitable place to drop the wwwroot files.根据Microsoft 文档,我知道这是因为 Windows 服务的工作目录是 C:\\Windows\\system32 文件夹,这不是放置 wwwroot 文件的合适位置。 How do I configure the location of the wwwroot folder?如何配置 wwwroot 文件夹的位置?

In the Startup.cs class Configure() function, I needed to add the following:在 Startup.cs 类的 Configure() 函数中,我需要添加以下内容:

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(workingDirectoryPath, "wwwroot"))
});

where 'workingDirectoryPath' was an absolute path to the directory where my service executable is, eg "C:\\MyService"其中“workingDirectoryPath”是我的服务可执行文件所在目录的绝对路径,例如“C:\\MyService”

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

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