找不到JS文件-无法加载资源:服务器的状态为404()

时间:2019-04-24 13:50:37

标签: javascript asp.net model-view-controller asp-net-core-spa-services

我无法弄清楚如何让我的网站加载脚本。我在_Layout.cshtml文件的部分中实现了这些脚本。

_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>

但是,对于我在底部添加的所有4个脚本,在运行时都出现此错误:

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

下面是我的文件夹结构。

folder structure

我引用的文件不正确吗?

1 个答案:

答案 0 :(得分:0)

Scripts文件夹应位于wwwroot文件夹中,以便服务器找到它们。

要从其他目录提供静态文件,请使用:

        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")
        });

在您的Startup.cs Configure方法中。

P.S这不会更改服务器的来源,但会添加一个附加目录来服务文件。