将ASP.Net Core Angular模板从版本8升级到9

10
我读的书籍从一开始就有第9版 (ASP.NET Core 3 and Angular 9 - Third Edition),并且也使用了Angular模板。我已经安装了最新的Visual Studio更新程序,适用于VS 2019.
Angular项目的模板版本为8,而我读的书是第9版。因此,我尝试升级我的Angular版本。执行 ng update 命令后得到以下结果:
@angular/cli 8.3.14 -> 9.0.3 ng update @angular/cli

@angular/core 8.2.12 -> 9.0.3 ng update @angular/core

@nguniversal/aspnetcore-engine 8.1.1 -> 8.2.6 ng update @nguniversal/aspnetcore-engine

我更新了cli和core。但是当我尝试更新第三个包时,出现以下错误:

C:\Users\user\Test\ClientApp>ng update @nguniversal/aspnetcore-engine
Using package manager: 'npm'
Collecting installed dependencies...
Found 39 dependencies.
Package '@nguniversal/aspnetcore-engine' is not a dependency.

我尝试运行应用程序,但我得到的是HTML。
Cannot GET /

或者是一个带有异常信息的页面:

System.TimeoutException: The Angular CLI process did not start listening for requests within the timeout period of 0 seconds. Check the log output for error information.
   at Microsoft.AspNetCore.SpaServices.Extensions.Util.TaskTimeoutExtensions.WithTimeout[T](Task`1 task, TimeSpan timeoutDelay, String message)
   at Microsoft.AspNetCore.SpaServices.Extensions.Proxy.SpaProxy.PerformProxyRequest(HttpContext context, HttpClient httpClient, Task`1 baseUriTask, CancellationToken applicationStoppingToken, Boolean proxy404s)
   at Microsoft.AspNetCore.Builder.SpaProxyingExtensions.<>c__DisplayClass2_0.<<UseProxyToSpaDevelopmentServer>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

尝试 -> dotnet run,然后导航到 localhost:5000。 - Joe Swindell
3个回答

0

当将Angular从v8升级到v9时,'ng update'会在main.ts中添加这行代码

export { renderModule, renderModuleFactory } from '@angular/platform-server';

只需将其删除,上述错误就会消失,检查此link


0
也许有点晚了,但也许这可以帮助其他人。我遇到了完全相同的问题。要使它工作,请在 package.json 中执行此操作。
更改:
"scripts":{
   ...
   "start": "ng serve"
   ...
}
 

"scripts":{
   ...
   "start": "echo Starting... && ng serve"
   ...
}
 

这段话来自Smatusan的评论。这是解释它为什么有效的原因。
"由于ng serve的初始构建时间超过了.NET SPA服务程序化超时窗口,所以这个问题经常发生,因此通过给它回声,.NET SPA服务接收到一些数据并认为它已连接。" - mikejunt
完整的GitHub问题可以在Angular.NET方面找到。

0

当我将Angular版本升级到11并仍在运行.NET Core 3.1时,我遇到了类似的问题。

我的解决方法:

完成Angular更新(确保所有节点模块也都是最新的) 安装.Net Core 5.0 右键单击解决方案->属性->应用程序:目标框架下拉菜单(更改为最近安装的5.0) 保存更改 管理Nuget包 -> 更新Microsoft.AspNetCore.SpaServices.Extensions到5+版本

然后我就能像往常一样启动我的调试会话了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接