简体   繁体   中英

How to call custom JS and CSS files from CSHTML in .Net core

I have a .net core project where the css, js and images are in different folders.

JS路径

CSS 路径

The index.cshtml is in "\TechBlog.Web\Views\Home"

索引cshtml路径

I'm trying to call the CSS and JS files from Index.cshtml. However, its not loading.

  @{
     ViewData["Title"] = "Home Page";
  }
  <!DOCTYPE html>
  <html lang="en">
  <head>
  //Load CSS
  <link rel="stylesheet" href="../css/all.css">
  </head>
  <body>
  //Load JS
  <script src="../js/Jquery3.4.1.min.js"></script>
  </body>

Please note that, there other line of codes. However, I just looking at, what I'm doing wrong for providing path.

I have added my JS and CSS files into WWWroot and using Visual Studio code. However, I don't see the files added in the folder getting reflected in the Visual Studio code explorer.

Am I providing the path incorrectly? Or there is something wrong? Thanks.

you should use this:

 <link rel="stylesheet" href="../../css/all.css">

Or

 <link rel="stylesheet" href="~/~/css/all.css">

you most change default static folder from wwwroot to root in startup.cs > Configure:

 app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(env.ContentRootPath),
        RequestPath = "/"
    });

then:

<link rel="stylesheet" href="~/css/all.css">

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