Search code examples
cssasp.netviewsrcshared

ASP.NET core shared view cannot access .js and .css libs


I am working on ASP.NET core 3.1. I want to do a simple application. I have a very basic problem but I cannot find a good way to solve it.

I have the menu of the application in a Shared view _Layout.cshtml. In this view i am loading .js and .css libs with:

<script src="assets/libs/jquery/dist/jquery.min.js"></script>

Everything is working fine, the menu is correctly displayed, and I can display my Index.cshtml with the @RenderBody().

The problem is that when I want to go to another page using:

<a asp-area="" asp-controller="Home" asp-action="Privacy" aria-expanded="false"><span>Privacy</span></a>

or

<a href="@Url.Action("Privacy", "Home")"><span>Privacy</span></a>

the page will be loaded trying to fetch the libs from :

<script src="Home/assets/libs/jquery/dist/jquery.min.js"></script>

And it won't find them.

I would like to know what is the best practice to avoid this issue.

Thank you for your help.


Solution

  • In asp .net core static files should be placed inside wwwroot folder. Thats why put static files inside wwwroot folder and give reference to _Layout.cshtml like this,

    <script src="~/JavaScript/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="~/CSS/styles.css"/>