简体   繁体   English

ASP.NET 5发布的输出不提供来自wwwroot的文件

[英]ASP.NET 5 published output doesn't serve files from wwwroot

I am having a great deal of trouble with the published version of my MVC 6 web application. 我的MVC 6 Web应用程序的发布版本给我带来很多麻烦。 It works fine when running from Visual Studio, but when running from the published output the static files aren't being read from the wwwroot folder. 从Visual Studio运行时,它工作正常,但从发布的输出运行时,不会从wwwroot文件夹中读取静态文件。

I know I'm supposed to include code examples with my question. 我知道我应该在问题中包含代码示例。 It is a lot of code and involves comparing my solution in Visual Studio to the published output. 这是很多代码,涉及将我在Visual Studio中的解决方案与发布的输出进行比较。 For my code example I've created a GitHub repo that reproduces this: https://github.com/TimothyK/Mvc6ServiceEg . 对于我的代码示例,我创建了一个复制此代码的GitHub存储库: https : //github.com/TimothyK/Mvc6ServiceEg

The project is basically File-New Project for an ASP.NET 5 Web Application. 该项目基本上是ASP.NET 5 Web应用程序的“文件新项目”。 However, eventually I want to get my project running as a self hosted Windows Service. 但是,最终我想让我的项目作为自托管Windows服务运行。 So there is a bit of code in this project that will eventually let it run as a Windows Service and manually calls the default Startup class. 因此,该项目中有一些代码,最终将使它作为Windows服务运行并手动调用默认的Startup类。 This code was extracted from this article: http://taskmatics.com/blog/host-asp-net-in-a-windows-service/ . 该代码摘自本文: http : //taskmatics.com/blog/host-asp-net-in-a-windows-service/ The code creates a DNX command called "run" in addition to the default "web" command. 除了默认的“ web”命令外,该代码还创建了一个名为“ run”的DNX命令。

As per the readme.md file the BuildScripts\\Publish.bat file will create a self contained folder for the project to be run from. 根据readme.md文件,BuildScripts \\ Publish.bat文件将为要从中运行的项目创建一个自包含文件夹。 After running the Publish.bat script the self hosted web server can be run using C:\\publish\\Mvc6ServiceEg\\approot\\run.cmd. 运行Publish.bat脚本后,可以使用C:\\ publish \\ Mvc6ServiceEg \\ approot \\ run.cmd运行自托管的Web服务器。

When running in this manner static files are not served up from the web server as expected. 当以这种方式运行时,无法从Web服务器提供预期的静态文件。 You can reproduce this by browsing to http://localhost:5000/images/Banner-02-VS.png . 您可以通过浏览到http:// localhost:5000 / images / Banner-02-VS.png来重现此内容 That works fine if you run the web app from Visual Studio, but gets a 404 error if running from the published run.cmd file. 如果您从Visual Studio运行Web应用程序,则效果很好,但如果从已发布的run.cmd文件运行,则会出现404错误。

I've figured out what is going wrong, but I don't know how to fix it. 我已经知道出了什么问题,但是我不知道如何解决。 The reason is that web server is not mapping to the correct folder. 原因是Web服务器未映射到正确的文件夹。 Static files should be hosted from the C:\\publish\\Mvc6ServiceEg\\wwwroot folder, which does contain the images\\Banner-02-VS.png file. 静态文件应位于C:\\ publish \\ Mvc6ServiceEg \\ wwwroot文件夹中,该文件夹确实包含images \\ Banner-02-VS.png文件。

The web server is actual hosting static files from C:\\publishMvc6ServiceEg\\approot\\packages\\Mvc6ServiceEg\\1.0.0\\root. Web服务器实际托管来自C:\\ publishMvc6ServiceEg \\ approot \\ packages \\ Mvc6ServiceEg \\ 1.0.0 \\ root的静态文件。 You can prove that by browsing to http://localhost:5000/Project_Readme.html , which does exist in that folder. 您可以通过浏览到该文件夹​​中存在的http:// localhost:5000 / Project_Readme.html来证明这一点。

There is a hosting.json file in the published output that should be telling the web server that the webroot is the wwwroot folder. 在发布的输出中有一个hosts.json文件,该文件应告诉Web服务器webroot是wwwroot文件夹。 However, the run.cmd execution path of the web application doesn't seem to be respecting that. 但是,Web应用程序的run.cmd执行路径似乎并不尊重这一点。

Can anyone tell me why this isn't mapping correctly? 谁能告诉我为什么映射不正确? Thanks in advance. 提前致谢。

Do you have the static files Nuget package installed? 您是否安装了静态文件Nuget软件包? http://www.nuget.org/packages/Microsoft.AspNet.StaticFiles/ http://www.nuget.org/packages/Microsoft.AspNet.StaticFiles/

Depending on which template you started with this may or may not already be enabled (it is not with the 'empty' one) 取决于您是从哪个模板开始的,此模板可能已启用,也可能尚未启用(它不是“空”模板)

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

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