简体   繁体   English

VS 更新后,带有 Node.js 的 ASP.NET Core 2.2 应用程序损坏

[英]ASP.NET Core 2.2 app with Node.js broke after VS update

note: the interesting bit is under [Edit 10/22/2020] below, the intro is just me being puzzled.注意:有趣的部分在下面的 [Edit 10/22/2020] 下,介绍只是让我感到困惑。

I think it was the VS update to 16.8.0 Preview 4.0.我认为这是对 16.8.0 Preview 4.0 的 VS 更新。 I am well aware .NET Core 2.2 was deprecated and is no longer supported.我很清楚 .NET Core 2.2 已被弃用,不再受支持。 I want to understand what is going on.我想了解发生了什么。

We inherited a web application that was supposed to be ASP.NET Core with MVC but as it appears it doesn't do much with that infrastructure and instead leans heavily on Node.js.我们继承了一个 Web 应用程序,该应用程序应该是带有 MVC 的 ASP.NET Core,但它似乎对该基础架构没有太大作用,而是严重依赖 Node.js。 Here's webpack.config.js :这是webpack.config.js

const path = require('path');

module.exports = {
    entry: {
        main: './ClientApp/index.js'
    },
    devtool: 'inline-source-map',
    stats: { modules: false },
    devServer: {
        hot: false
    },
    watchOptions: {
        ignored: /node_modules/
    },
    output: {
        path: path.join(__dirname, '/wwwroot/js/'),
        filename: 'site.js',
        publicPath: '/'
    }
};

An entry point ./ClientApp/index.js is declared and until recently this seemed indeed what was executed when one started the application in Visual Studio with IIS Express and Chrome.声明了一个入口点 ./ClientApp/index.js ,直到最近这似乎确实是在使用 IIS Express 和 Chrome 在 Visual Studio 中启动应用程序时执行的。

However, since my update I get Index.cshtml instead.但是,自从我更新后,我得到了Index.cshtml Which is exactly what I would expect from an ASP.NET application with MVC.这正是我对带有 MVC 的 ASP.NET 应用程序的期望。

One remarkable thing I noticed is that under dependencies I no longer see the any/npm branch that used to be there.我注意到的一件值得注意的事情是,在依赖项下,我不再看到曾经存在的 any/npm 分支。

My co-workers who are still on Visual Studio 2019 stable still get the old behavior, with index.js kicking in. It looks like request routing behavior has changed.我的同事仍然在 Visual Studio 2019 稳定版上仍然得到旧的行为,使用index.js 。看起来请求路由行为已经改变。

Re-installing dotnet SDK 2.2 and Node.js with subsequent "npm install" does not fix it, I still get the default page Index.cshtml .使用后续的“npm install”重新安装 dotnet SDK 2.2 和 Node.js 并不能修复它,我仍然得到默认页面Index.cshtml

Question: What exactly made the application execute index.js rather than Index.cshtml ?问题:究竟是什么让应用程序执行index.js而不是Index.cshtml I do not find any references in the code to index.js apart from the declaration in webpack.config.js .除了webpack.config.js的声明之外,我在代码中没有找到任何对index.jswebpack.config.js I would expect to find something that starts node.exe with index.js as an argument, or some statement that wraps that.我希望找到一些以index.js为参数的node.exe开头的东西,或者一些包装它的语句。

I tried to get it working by installing NuGet packages Node.js and Microsoft.AspNetCore.NodeServices, which were never there before.我试图通过安装 NuGet 包 Node.js 和 Microsoft.AspNetCore.NodeServices 来让它工作,这些包以前从未出现过。 But it does not restore the desired behavior.但它不会恢复所需的行为。

[Edit] [编辑]

It appears it does not matter what I enter for the main entry point in webpack.config.js , it only needs to comply with the schema but it is never even looked for at runtime.看起来我在webpack.config.js的主入口点输入什么并不重要,它只需要符合架构,但它甚至在运行时都不会被查找。 I found a way to display the console to see some output.我找到了一种显示控制台以查看一些输出的方法。 I got rid of some warnings and errors but the result is still the same.我摆脱了一些警告和错误,但结果仍然相同。 It may not be routing after all, it may have been the same page all the time, just now dysfunctional and looking very different as a result.它可能根本不是路由,它可能一直是同一个页面,只是现在功能失调并且因此看起来非常不同。 I do not know.我不知道。 Here's the console output, I hope it will provide some alert to more knowledgeable folks:这是控制台输出,我希望它可以为更多知识渊博的人提供一些警报:

info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\MaatM\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Hosting environment: Development
Content root path: D:\Projects\ProjectName
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.NodeServices[0]
      webpack built 33af367a37b276ede2b3 in 2029ms
info: Microsoft.AspNetCore.NodeServices[0]
      i ´¢ówdm´¢ú: Hash: 33af367a37b276ede2b3
      Version: webpack 4.29.5
      Time: 2029ms
      Built at: 2020-10-16 14:22:53
        Asset      Size  Chunks             Chunk Names
      site.js  6.71 MiB    main  [emitted]  main
      Entrypoint main = site.js
info: Microsoft.AspNetCore.NodeServices[0]
      i ´¢ówdm´¢ú: Compiled successfully.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'Page: /Index'
info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[3]
      Route matched with {page = "/Index"}. Executing page /Index
info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[101]
      Executing handler method ProjectName.Pages.IndexModel.OnGet with arguments ((null)) - ModelState is Valid
info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[102]
      Executed handler method OnGet, returned result .
info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[103]
      Executing an implicit handler method - ModelState is Valid
info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[104]
      Executed an implicit handler method, returned result Microsoft.AspNetCore.Mvc.RazorPages.PageResult.
info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[4]
      Executed page /Index in 63.434ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'Page: /Index'
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 217.9321ms 200 text/html; charset=utf-8
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/~/css/site.css?123
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/~/lib/bootstrap/dist/css/bootstrap.css
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/~/lib/jquery/dist/jquery.js
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/~/js/site.js
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/~/lib/bootstrap/dist/js/bootstrap.bundle.js
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 31.0267ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 31.0331ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 19.6686ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 52.5483ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 52.5487ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/~/css/site.css?123
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 5.9602ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/~/lib/jquery/dist/jquery.js
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 4.1753ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/~/lib/bootstrap/dist/js/bootstrap.bundle.js
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 3.914ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET https://localhost:5001/~/js/site.js
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 4.3371ms 404

Some puzzling things: I see some garbled characters under the second Microsoft.AspNetCore.NodeServices[0], then the same thing a bit further down.一些令人费解的事情:我在第二个 Microsoft.AspNetCore.NodeServices[0] 下看到一些乱码,然后再往下看也是一样。 In between it says Entrypoint main = site.js which I did not declare.在它之间说Entrypoint main = site.js我没有声明。

[Edit 10/22/2020] [编辑 10/22/2020]

Well.好。 this is exciting.这真让人兴奋。 Thank you M Hornbacher for the comment.感谢 M Hornbacher 的评论。 The "link tag to site.js" was not in Index.cshtml, I found it in _Layout.cshtml and it looked like this: “指向 site.js 的链接标签”不在 Index.cshtml 中,我在 _Layout.cshtml 中找到它,它看起来像这样:

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

which struck me as odd.这让我觉得很奇怪。 I was not familiar with the ~ syntax, I figured I would have written it like this instead:我不熟悉 ~ 语法,我想我应该这样写:

<script type="text/javascript" src="/js/site.js"></script>

So I changed it, ran it and I got some of the old desired behavior back!所以我改变了它,运行它,我得到了一些旧的期望行为! I then looked for more occurrences of the ~/ construct, changed them and instantly got (nearly) all of the desired behavior back.然后我寻找更多出现的 ~/ 构造,更改它们并立即(几乎)获得所有所需的行为。

Apparently the interpretation of ~/ broke, which as I understand it is an ASP.NET thing that gets replaced with the root path on the server.显然 ~/ 的解释中断了,据我所知,这是一个 ASP.NET 的东西,它被服务器上的根路径替换了。 Or, more likely, what the app considers root changed.或者,更有可能的是,应用程序认为 root 的内容发生了变化。 So now I am wondering where does it say what is to be root?所以现在我想知道它在哪里说什么是根?

By the way, I am on Visual Studio 2019 16.8.0 Preview 5.0 now if that could matter.顺便说一句,如果这很重要,我现在使用的是 Visual Studio 2019 16.8.0 Preview 5.0。 The app is still .NET Core 2.2.该应用程序仍然是 .NET Core 2.2。

I imagine you know this but, ~ typically stands for the home directory.我想你知道这一点,但是~通常代表主目录。 On a linux or mac you type cd ~ and it will take you to the current users directory.在 linux 或 mac 上,您键入cd ~它将带您到当前用户目录。

Like you said ~ could be ASP.net specific, I don't usually see it on projects that deploy to linux, it's too relative.就像你说的~可能是 ASP.net 特定的,我通常不会在部署到 linux 的项目中看到它,它太相对了。

That said there isn't any C#/ASP.net code to see here.也就是说这里没有任何 C#/ASP.net 代码可以看到。 If it actually works on the production server but not locally you might have issues getting to work in production because the ~ is now missing.如果它实际上在生产服务器上运行但不在本地运行,则在生产中可能会遇到问题,因为~现在丢失了。

You might want to research if prod really requires ~ in the path and if so why.您可能想研究 prod 是否真的需要~在路径中,如果需要,为什么。

So now I am wondering where does it say what is to be root?

Based on this:基于此:

output: {
        path: path.join(__dirname, '/wwwroot/js/'),
        filename: 'site.js',
        publicPath: '/'
    }

and that __dirname Can change based on the file it is run in I believe.我相信__dirname可以根据它运行的文件进行更改。 Your js file site.js is going to ${__dirname/wwwroot/js/}您的 js 文件site.js将转到${__dirname/wwwroot/js/}

In order for this to work为了使这个工作

<script type="text/javascript" src="/js/site.js"></script>

Your root directory for the project looks like it is ${__dirname}/wwwroot/您的项目根目录看起来像${__dirname}/wwwroot/

I also notice in your output you have:我还注意到在你的输出中你有:

Content root path: D:\Projects\ProjectName

That could be the same as __dirname , thats a guess, it's not possible to determine more with the info given.这可能与__dirname相同,这是一个猜测,根据给定的信息无法确定更多信息。

I would be curious to know if there is a index/server.js file around.我很想知道周围是否有 index/server.js 文件。

Here are some references that discuss determining the root of a node app:以下是一些讨论确定节点应用程序根的参考资料:

Determine project root from a running node.js application 从正在运行的 node.js 应用程序确定项目根目录

https://www.abeautifulsite.net/determining-your-apps-base-directory-in-nodejs https://www.abeautifulsite.net/determining-your-apps-base-directory-in-nodejs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM