Search code examples
javascriptasp.netmodel-view-controllerasp-net-core-spa-services

Cannot find JS files -- Failed to load resource: the server responded with a status of 404 ()


I cannot figure out how to get my site to load my scripts. I have these scripts implemented in the section of my _Layout.cshtml file.

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - HighYieldSecondaryTradingApp</title>

<environment include="Development">
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</environment>
<environment exclude="Development">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
          asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
    <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>
@*added by sean*@
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/sammy-0.7.4.js"></script>
<script src="~/Scripts/bootstrap.js"></script>
<script src="~/scripts/layout-routing.js"></script>
</head>

However, for all 4 scripts I've added at the bottom, I'm getting this error on runtime:

Failed to load resource: the server responded with a status of 404 ()

Below is my folder structure.

folder structure

Am I referencing the files incorrectly?


Solution

  • Scripts folder should be inside the wwwroot folder in order for the server to find them.

    To serve static files from a different directory, use:

    app.UseStaticFiles(new StaticFileOptions()
    {
        // Where the files are physicly located
        FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Scripts")),
        // What relative url to serve the files from
        RequestPath = new PathString("/scripts")
    });
    

    in your Startup.cs Configure Method.

    P.S This will not change where the server is serving from, but will add an additional directory to serve files from.