问题描述
注意:有趣的是在下面的[Edit 10/22/2020]下,简介只是让我感到困惑。
我认为这是VS对16.8.0 Preview 4.0的更新。我很清楚.NET Core 2.2已被弃用,不再受支持。我想了解发生了什么。
我们继承了一个Web应用程序,该应用程序应该是带有MVC的ASP.NET Core,但看起来它与该基础结构并没有多大关系,而是严重依赖于Node.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: '/'
}
};
声明了一个入口点./ClientApp/index.js,直到最近,这似乎确实是在使用IIS Express和Chrome在Visual Studio中启动该应用程序时执行的。
但是,自更新以来,我得到的是Index.cshtml
。这正是我期望从带有MVC的ASP.NET应用程序中得到的结果。
我注意到的一件非凡的事情是,在依赖项下,我不再看到曾经存在的any / npm分支。
我的那些仍在Visual Studio 2019稳定版中的同事仍然出现了旧的行为,index.js
开始起作用。请求路由行为似乎已更改。
使用后续的“ npm install”重新安装dotnet SDK 2.2和Node.js无法解决该问题,我仍然得到默认页面Index.cshtml
。
问题:究竟是什么使应用程序执行index.js
而不是Index.cshtml
?除了index.js
中的声明以外,我在代码中没有找到对webpack.config.js
的引用。我希望找到以node.exe
作为参数的index.js
开头的东西,或包装它的语句。
我试图通过安装NuGet程序包Node.js和Microsoft.AspNetCore.nodeservices使其工作,而这是以前从未有过的。但这不能恢复预期的行为。
[编辑]
看来我在webpack.config.js
中为主要入口点输入什么都没有关系,它只需要遵守模式,甚至在运行时都不会寻找它。我找到了一种显示控制台以查看一些输出的方法。我摆脱了一些警告和错误,但结果仍然相同。毕竟它可能不是路由,可能一直都是同一页,只是现在功能不正常,结果看起来很不一样。我不知道。这是控制台的输出,我希望它将为更多知识渊博的人提供一些警报:
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
有些令人费解的事情:我在第二个Microsoft.AspNetCore.nodeservices [0]下看到一些乱码,然后同样的事情又往下走了。中间有Entrypoint main = site.js
,我没有声明。
[编辑10/22/2020]
好吧。这真让人兴奋。谢谢M Hornbacher的评论。 “指向site.js的链接标签”不在Index.cshtml中,我在_Layout.cshtml中找到了它,它看起来像这样:
<script src="~/js/site.js" asp-append-version="true"></script>
这让我很奇怪。我不熟悉〜语法,我以为我会这样写:
<script type="text/javascript" src="/js/site.js"></script>
因此,我对其进行了更改,运行并获得了一些旧的期望行为!然后,我查找了〜/构造的更多实例,对其进行了更改,并立即(几乎)收回了所有所需的行为。
很显然,〜/的解释已经中断,据我所知,这是一台ASP.NET,已被服务器上的根路径替换。或者,更有可能的是,应用程序认为根目录已更改。所以现在我想知道它在哪里说要成为根?
顺便说一句,如果可能的话,我现在在Visual Studio 2019 16.8.0 Preview 5.0上。该应用程序仍然是.NET Core 2.2。
解决方法
我想您知道这一点,但是~
通常代表主目录。在Linux或Mac上,键入cd ~
,它将带您到当前用户目录。
就像您说的~
可能是特定于ASP.net的一样,在部署到linux的项目中我通常不会看到它,因为它太相对了。
那表示这里没有任何C#/ ASP.net代码。如果它实际上在生产服务器上有效,但不能在本地服务器上工作,则可能因为在~
上丢失而在生产中无法正常工作。
您可能想研究路径中确实需要~
的产品,如果需要,为什么?
So now I am wondering where does it say what is to be root?
基于此:
output: {
path: path.join(__dirname,'/wwwroot/js/'),filename: 'site.js',publicPath: '/'
}
那
__dirname
可以根据运行中的文件进行更改。
您的js文件site.js
将进入${__dirname/wwwroot/js/}
为了使其正常工作
<script type="text/javascript" src="/js/site.js"></script>
项目的根目录看起来像是
${__dirname}/wwwroot/
在您的输出中,我还注意到您有
Content root path: D:\Projects\ProjectName
这可能与__dirname
相同,这是一个猜测,使用给出的信息无法确定更多信息。
我很想知道周围是否有index / server.js文件。
以下是讨论确定节点应用程序根目录的一些参考文献:
Determine project root from a running node.js application
https://www.abeautifulsite.net/determining-your-apps-base-directory-in-nodejs