Search code examples
c#.netasp.net-corevisual-studio-codekestrel-http-server

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 bootstrap.css, jquery.js and bootstrap.js don't load (404).

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

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

Thanks


Solution

  • 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,
      "copyToOutput": 
      [
        "wwwroot",
        "Views",
        "appsettings.json",
        "web.config"
      ]
    },
    "publishOptions": {
      "include": [
        "wwwroot",
        "Views",
        "appsettings.json",
        "web.config"
      ]
    },
    

    Though usually it should be necessary to add wwwroot to copyToOutput