簡體   English   中英

如何從 CSHTML in.Net core 調用自定義 JS 和 CSS 文件

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

我有一個 .net 核心項目,其中 css、js 和圖像位於不同的文件夾中。

JS路徑

CSS 路徑

index.cshtml 位於“\TechBlog.Web\Views\Home”中

索引cshtml路徑

我正在嘗試從 Index.cshtml 調用 CSS 和 JS 文件。 但是,它沒有加載。

  @{
     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>

請注意,還有其他代碼行。 但是,我只是在看,我在提供路徑方面做錯了什么。

我已將我的 JS 和 CSS 文件添加到 WWWroot 並使用 Visual Studio 代碼。 但是,我沒有看到文件夾中添加的文件反映在 Visual Studio 代碼資源管理器中。

我提供的路徑不正確嗎? 還是有什么不對? 謝謝。

你應該使用這個:

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

或者

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

您最常在 startup.cs > 配置中將默認 static 文件夾從 wwwroot 更改為 root:

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

然后:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM