在 IIS 上使用 Asp.Net Core 2.1.1 发布 Angular 6 应用程序

Publishing an Angular 6 app with Asp.Net Core 2.1.1 on IIS

使用 Visual Studio 2017 (15.7.5) 应用程序正常运行。但是,发布到 IIS 7.5 时,当我尝试访问浏览器时出现以下错误:

An unhandled exception occurred while processing the request.

AggregateException: One or more errors occurred. (One or more errors occurred. (The NPM script 'start' exited without indicating that the Angular CLI was listening for requests. The error output was: npm ERR! path C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json

npm ERR! code ENOENT

npm ERR! errno -4058

npm ERR! syscall open

npm ERR! enoent ENOENT: no such file or directory, open 'C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json'

npm ERR! enoent This is related to npm not being able to find a file.

npm ERR! enoent

npm ERR! A complete log of this run can be found in:

npm ERR! C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache_logs18-08-01T14_50_15_092Z-debug.log

))

System.Threading.Tasks.Task.GetResultCore(bool waitCompletionNotification)

InvalidOperationException: The NPM script 'start' exited without indicating that the Angular CLI was listening for requests. The error output was: npm ERR! path C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json

npm ERR! code ENOENT

npm ERR! errno -4058

npm ERR! syscall open

npm ERR! enoent ENOENT: no such file or directory, open 'C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json'

npm ERR! enoent This is related to npm not being able to find a file.

npm ERR! enoent

npm ERR! A complete log of this run can be found in:

npm ERR! C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache_logs18-08-01T14_50_15_092Z-debug.log

Microsoft.AspNetCore.SpaServices.AngularCli.AngularCliMiddleware.StartAngularCliServerAsync(string sourcePath, string npmScriptName, ILogger logger)

Stack Query Cookies Headers

AggregateException: One or more errors occurred. (One or more errors occurred. (The NPM script 'start' exited without indicating that the Angular CLI was listening for requests. The error output was: npm ERR! path C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json

npm ERR! code ENOENT npm ERR! errno -4058 npm ERR! syscall open npm ERR! enoent ENOENT: no such file or directory, open 'C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json'

npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent

npm ERR! A complete log of this run can be found in:

npm ERR! C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache_logs18-08-01T14_50_15_092Z-debug.log

))

System.Threading.Tasks.Task.GetResultCore(bool waitCompletionNotification)

Microsoft.AspNetCore.SpaServices.Extensions.Util.TaskTimeoutExtensions.WithTimeout(Task task, TimeSpan timeoutDelay, string message)

Microsoft.AspNetCore.SpaServices.Extensions.Proxy.SpaProxy.PerformProxyRequest(HttpContext context, HttpClient httpClient, Task baseUriTask, CancellationToken applicationStoppingToken, bool proxy404s)

Microsoft.AspNetCore.Builder.SpaProxyingExtensions+<>c__DisplayClass2_0+<b__0>d.MoveNext()

Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)

Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Show raw exception details

InvalidOperationException: The NPM script 'start' exited without indicating that the Angular CLI was listening for requests. The error output was: npm ERR! path C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json

npm ERR! code ENOENT npm ERR! errno -4058 npm ERR! syscall open npm ERR! enoent ENOENT: no such file or directory, open 'C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json'

npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent

npm ERR! A complete log of this run can be found in:

npm ERR! C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache_logs18-08-01T14_50_15_092Z-debug.log

Microsoft.AspNetCore.SpaServices.AngularCli.AngularCliMiddleware.StartAngularCliServerAsync(string sourcePath, string npmScriptName, ILogger logger)

Show raw exception details

System.InvalidOperationException: The NPM script 'start' exited without indicating that the Angular CLI was listening for requests. The error output was: npm ERR! path C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json

npm ERR! code ENOENT

npm ERR! errno -4058

npm ERR! syscall open

npm ERR! enoent ENOENT: no such file or directory, open 'C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json'

npm ERR! enoent This is related to npm not being able to find a file.

npm ERR! enoent

npm ERR! A complete log of this run can be found in:

npm ERR! C:\Windows\system32\config\systemprofile\AppData\Roaming\npm-cache_logs18-08-01T14_50_15_092Z-debug.log

---> System.IO.EndOfStreamException: Attempted to read past the end of the stream.

at Microsoft.AspNetCore.SpaServices.AngularCli.AngularCliMiddleware.StartAngularCliServerAsync(String sourcePath, String npmScriptName, ILogger logger)

--- End of inner exception stack trace ---

at Microsoft.AspNetCore.SpaServices.AngularCli.AngularCliMiddleware.StartAngularCliServerAsync(String sourcePath, String npmScriptName, ILogger logger)

生成的日志如下:

info it worked if it ends with ok

verbose cli [ 'D:\Programas\nodejs\node.exe',

verbose cli 'D:\Programas\nodejs\node_modules\npm\bin\npm-cli.js',

verbose cli 'run',

verbose cli 'start',

verbose cli '--',

verbose cli '--port',

verbose cli '52476' ]

info using npm@5.6.0

info using node@v8.11.3

verbose stack Error: ENOENT: no such file or directory, open 'C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json'

verbose cwd C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp

verbose Windows_NT 6.1.7601

verbose argv "D:\Programas\nodejs\node.exe" "D:\Programas\nodejs\node_modules\npm\bin\npm-cli.js" "run" "start" "--" "--port" "52476"

verbose node v8.11.3

verbose npm v5.6.0

error path C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json

error code ENOENT

error errno -4058

error syscall open

error enoent ENOENT: no such file or directory, open 'C:\Sites\NewPro\Microsum.NewPro.ERP\ERPApp\package.json'

error enoent This is related to npm not being able to find a file.

verbose exit [ -4058, true ]

非常欢迎任何建议。

在 Visual Studio 中,在解决方案资源管理器上,select 文件夹 ClientApp[= 中的文件 package.json 16=] 然后将 属性 复制到输出目录 更改为 始终复制 .

我能够通过忽略 visual studio 的标准视觉模板并创建一个 AspNet Web 应用程序项目然后创建 angular 应用程序来解决。

我放the project code in github给有同样问题的人使用

我为想要手动构建项目的任何人生成了 a detailed step-by-step with prints

回复晚了,但如果有人仍然运行遇到与更高版本的 .NET 类似的问题Core/Angular SPA;

我已经使用 .NET Core 3.1 生成了空白模板,它是 Angular SPA 模板,在开发模式下总是 运行 成功,但在发布时会失败并出现与上述相同的错误。这对我来说总是很奇怪,因为它甚至不应该 寻找 package.json 如果你已经发布了应用程序 - 它应该加载静态文件。

问题出在它构建的开发环境上——默认情况下,我的 ASPNETCORE_ENVIRONMENT 变量设置为 Development,这意味着 Startup.cs 逻辑正在读取所有 if(env.IsDevelopment()) 检查为真,即使在发布之后也是如此。最简单的解决方法是在 VS 项目的“属性”>“发布配置文件”下创建一个新的发布配置文件。将其命名为 Production.pubxml 或其他名称并向其添加以下元素:<EnvironmentName>Production</EnvironmentName>。然后,当您从构建 > 发布菜单发布时,select 下拉菜单中的文件。这会将特定于此应用程序的环境变量添加到 web.config 文件,从而允许 SpaServices 中间件实际完成其工作,而不是尝试启动节点服务器。

或者,您可以只添加

<environmentVariables>
   <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
</environmentVariables>

到已发布的 web.config 文件,在 <aspNetCore 元素内,默认情况下是自关闭的。这里的缺点是您每次发布应用程序时都需要重新添加它,直到您创建发布配置文件。