BanksySan BanksySan - 2 years ago 69
C# Question

Some file not served when run via Visual Studio Code

I have a 'Hello World' Kestrel server generated by Yeoman (as described here).

yo aspnet

When I run the site via the command line everything work well:

dotnet run

If it's run through Visual Studio Code though
don't load (404).

Looking at the F12 tools, they are actually coming from different locations. When run via
dotnet run
they come from
, but when run through Visual Studio Code the browser is looking for them in a local folder

a. Why are the files being sourced differently depending on how I run the site?
b. How do I fix this?


Answer Source

When you run it from command line, it runs in production mode (no ASPNETCORE_ENVIRONMENT variable set. When you run it from Visual Studio it sets the ASPNETCORE_ENVIRONMENT to Development.

Inside your Razor files, you have a <environments> section which controls which files are served in which production mode. Depending on your environment/OS, you need to set the variable differently. i.e. in Linux you'd need to run ASPNETCORE_ENVIRONMENT=Development dotnet run.

As for the reason why you get 404 when running in development mode, you probably need to copy over the wwwroot folder to your output directory, with this entry in your project.json.

"buildOptions": {
  "emitEntryPoint": true,
  "preserveCompilationContext": true,
"publishOptions": {
  "include": [

Though usually it should be necessary to add wwwroot to copyToOutput

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download