ASP.NET Core 2.0:找不到静态文件(site.min.js) [英] ASP.NET Core 2.0: static file (site.min.js) not found

查看:65
本文介绍了ASP.NET Core 2.0:找不到静态文件(site.min.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Visual Studio 2017中的发布配置文件来部署ASP.NET Core Web应用程序.所有内容均已复制到目标文件夹,并且工作正常,但"site.js"文件除外.在目标文件夹中,我在wwwroot/js文件夹中看到site.js和site.min.js,但后者为空.我在浏览器中也收到控制台错误:

I want to deploy an ASP.NET Core web application via publishing profiles in Visual Studio 2017. Everything is copied to the destination folder and works fine, except the "site.js" file. In the destination folder, I see in the wwwroot/js folder both site.js and site.min.js, but the latter is empty. I also get a console error in the browser:

Failed to load resource: the server responded with a status of 404 (Not Found) site.min.js

Failed to load resource: the server responded with a status of 404 (Not Found) site.min.js

但是在页面源代码中,我看到文件已正确加载:

In page source however I see the file loaded correctly:

<script src="/wwwroot/js/site.min.js"></script>

Program.cs文件:

Program.cs file:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
             .UseKestrel()
             .UseContentRoot(Directory.GetCurrentDirectory())
             .UseIISIntegration()
             .UseStartup<Startup>()
             .Build();

        host.Run();
    }
}

Startup.cs中的服务配置:

Service configuration in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });    
}

_Layout.cshtml:

_Layout.cshtml:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Dashboard</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://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/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>
</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">Home</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a asp-area="" asp-controller="Home" asp-action="Create">View orders</a></li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; 2018 - test app</p>
        </footer>
    </div>

    <environment include="Development">
        <script src="~/lib/jquery/dist/jquery.js"></script>
        @*<script src="~/lib/jquery/jquery-validation/dist/jquery.validate.js"></script>*@
        <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
        <script src="~/js/site.js"></script>
    </environment>
    <environment exclude="Development">
        <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.0.min.js"
                asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                asp-fallback-test="window.jQuery"
                crossorigin="anonymous"
                integrity="sha384-K+ctZQ+LL8q6tP7I94W+qzQsfRV2a+AfHIi9k8z8l9ggpc8X+Ytst4yBo/hH+8Fk">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/bootstrap.min.js"
                asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
                asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
                crossorigin="anonymous"
                integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa">
        </script>
        <script src="~/wwwroot/js/site.min.js" asp-append-version="false"></script>
    </environment>

    @RenderSection("Scripts", required: false)
</body>
</html>

site.js位于wwwroot/js文件夹中(我没有移动或重新创建它). 我也没有更改bundleconfig.json.其内容如下:

The site.js is in the wwwroot/js folder (I didn't move it or recreate it). I didn't change the bundleconfig.json either. Its content is the following:

[
  {
    "outputFileName": "wwwroot/css/site.min.css",
    // An array of relative input file paths. Globbing patterns supported
    "inputFiles": [
      "wwwroot/css/site.css"
    ]
  },
  {
    "outputFileName": "wwwroot/js/site.min.js",
    "inputFiles": [
      "wwwroot/js/site.js"
    ],
    // Optionally specify minification options
    "minify": {
      "enabled": true,
      "renameLocals": true
    },
    // Optionally generate .map file
    "sourceMap": false
  }
]

我错过了什么吗? 预先谢谢你.

Is it something that I am missing out? Thank you in advance.

更新:

我更改了site.min.js脚本标签:

I changed the site.min.js script tag:

<script src="~/js/site.min.js" asp-append-version="true"></script>

我不再收到404错误,但是site.min.js仍然为空,因此是一个缩小问题.

I no longer get the 404 error, but the site.min.js is still empty and therefore is a minification issue.

更新2:

site.js内容:

site.js content:

$('#chkMinId').change(function () {
    var checked = $(this).is(':checked');
    var target = $('#txtMinId');
    console.log(checked ? 'yes' : 'no');

    if (!checked) {
        target.prop('disabled', true);
    } else {
        target.removeAttr('disabled');
    }
});

$('#chkMaxId').change(function () {
    var checked = $(this).is(':checked');
    var target = $('#txtMaxId');
    console.log(checked ? 'yes' : 'no');

    if (!checked) {
        target.prop('disabled', true);
    } else {
        target.removeAttr('disabled');
    }
});

推荐答案

要解决site.min.js 404错误:

将您的site.min.js脚本标签更改为

To resolve the site.min.js 404 error:

Change your site.min.js script tag to

<script src="~/js/site.min.js" asp-append-version="true"></script>

wwwroot指示根"在Web服务器而不是文件系统上的位置.

The wwwroot indicates where the 'root' is on the web server rather than the filesystem.

您必须安装一个名为 BuildBundlerMinifier 的nuget程序包.安装此软件包后,我看到site.min.js文件填充了site.js中的代码.

You have to install a nuget package called BuildBundlerMinifier. After installing this package, I saw my site.min.js file get populated with the code in site.js.

我通过阅读此帮助文档发现了这一点:

I found this from reading this helpdoc: Build-time execution of bundling and minification

这篇关于ASP.NET Core 2.0:找不到静态文件(site.min.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆