简体   繁体   中英

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

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. For my code example I've created a GitHub repo that reproduces this: https://github.com/TimothyK/Mvc6ServiceEg .

The project is basically File-New Project for an ASP.NET 5 Web Application. However, eventually I want to get my project running as a self hosted Windows Service. 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. This code was extracted from this article: 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.

As per the readme.md file the BuildScripts\\Publish.bat file will create a self contained folder for the project to be run from. After running the Publish.bat script the self hosted web server can be run using C:\\publish\\Mvc6ServiceEg\\approot\\run.cmd.

When running in this manner static files are not served up from the web server as expected. You can reproduce this by browsing to 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.

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. Static files should be hosted from the C:\\publish\\Mvc6ServiceEg\\wwwroot folder, which does contain the images\\Banner-02-VS.png file.

The web server is actual hosting static files from 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.

There is a hosting.json file in the published output that should be telling the web server that the webroot is the wwwroot folder. However, the run.cmd execution path of the web application doesn't seem to be respecting that.

Can anyone tell me why this isn't mapping correctly? Thanks in advance.

Do you have the static files Nuget package installed? 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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