Home > OS >  Visual Studio Code doesn't add CSS Stylesheets and JS-Files to HTML code
Visual Studio Code doesn't add CSS Stylesheets and JS-Files to HTML code

Time:03-24

I migrated my project from Visual Studio Enterprise to Visual Studio Code and now my CSS and JS-Files are not being embedded in the HTML-File anymore. There are no error-messages. I think the problem is, that VS sees the links as webpages on my website and searches for them and doesn't find them. I come to this conclusion because of the messages in the debug console.

My code looks like this:

@model LoginModel;
@{
    ViewData["Title"] = "Home Page";
 }
<head>
    <link   rel="stylesheet" href="../wwwroot/css/Home.css"
      type="text/css" />
    <link rel="stylesheet" href="../wwwroot/css/overview.css"
       type="text/css" />

</head>
<body>
      **more HTML code here **
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="${workspaceFolder}/NXM_Web_Client/wwwroot/js/login.js"></script>

The Debug console states that:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET http://localhost:5000/wwwroot/css/Home.css
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 21.0984ms 404 info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET http://localhost:5000/${workspaceFolder}/NXM_Web_Client/wwwroot/js/login.js
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 0.2092ms 404

The same message comes for all CSS and JS files. I allready tried marking the files as static, this didn't work.

CodePudding user response:

Make sure that your files are in same folder where you have saved your main html file.Just tell me after trying did it worked.

CodePudding user response:

While ../ in a URL means "Go to the parent segment of the path", you can't go above the server root.

wwwroot is the root.

You are trying to go from / to a level above / (which is impossible) and then back into / which leaves you in /wwwroot.

Don't do that.

Either use a relative path which doesn't go past the root (css/Home.css) or use an absolute path which starts at the root (/css/Home.css).

  • Related