1.错误再现

利用VS2019预览版创建ASP.NET Core 的单页面Web程序

创建后直接运行,出现如下错误

Ensure that 'npm' is installed and can be found in one of the PATH directories.

2.相关资料

找到以下资料

https://github.com/aspnet/AspNetCore/issues/5263

https://github.com/aspnet/AspNetCore.Docs/blob/master/aspnetcore/host-and-deploy/aspnet-core-module/samples_snapshot/2.x/CurrentDirectoryHelpers.cs

3.解决方案

添加类CurrentDirectoryHelpers,代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace WebApplication6
{
public class CurrentDirectoryHelpers
{
internal const string AspNetCoreModuleDll = "aspnetcorev2_inprocess.dll"; [System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string lpModuleName); [System.Runtime.InteropServices.DllImport(AspNetCoreModuleDll)]
private static extern int http_get_application_properties(ref IISConfigurationData iiConfigData); [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
private struct IISConfigurationData
{
public IntPtr pNativeApplication;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzFullApplicationPath;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.BStr)]
public string pwzVirtualApplicationPath;
public bool fWindowsAuthEnabled;
public bool fBasicAuthEnabled;
public bool fAnonymousAuthEnable;
} public static void SetCurrentDirectory()
{
try
{
// Check if physical path was provided by ANCM
var sitePhysicalPath = Environment.GetEnvironmentVariable("ASPNETCORE_IIS_PHYSICAL_PATH");
if (string.IsNullOrEmpty(sitePhysicalPath))
{
// Skip if not running ANCM InProcess
if (GetModuleHandle(AspNetCoreModuleDll) == IntPtr.Zero)
{
return;
} IISConfigurationData configurationData = default(IISConfigurationData);
if (http_get_application_properties(ref configurationData) != 0)
{
return;
} sitePhysicalPath = configurationData.pwzFullApplicationPath;
} Environment.CurrentDirectory = sitePhysicalPath;
}
catch
{
// ignore
}
}
}
}

在Main方法中调用

运行,发现神奇般的好啦!!!

转载于:https://www.cnblogs.com/zhangqibao/p/10839226.html

最新文章

  1. MySql事务概述
  2. Floyd算法(一)之 C语言详解
  3. android AsyncTask介绍(转)
  4. poj 1114 完全背包 dp
  5. 目前主流的国产WebApp框架
  6. ui学习笔记---第十五天数据库
  7. linux下的chmod,chown和chgrp
  8. Flexible 弹性盒子模型之CSS flex-direction
  9. DelphiXE4- System.IOUtils.TDirectory笔记查询后缀名为dll的文件
  10. QLGame 2d Engine源码地址
  11. EDM(邮件营销)
  12. 共享内存操作类(C#源码)
  13. Linux分配给该用户没有权限登陆
  14. Grunt实现自动化单元测试
  15. 团队作业4——第一次项目冲刺(Alpha版本)2017.4.27
  16. ubuntu 14.04 vim YoucompleteMe 代码自动补全工具安装
  17. [转]SVN使用log,list,cat,diff查看所有及特定文件版本信息
  18. codeforces#1136E. Nastya Hasn't Written a Legend(二分+线段树)
  19. fastclick的介绍和使用
  20. 自学Linux Shell9.2-基于Red Hat系统工具包存在两种方式之一:RPM包

热门文章

  1. PTA | 1014 福尔摩斯的约会 (20分)
  2. PTA | 1005 继续(3n+1)猜想 (25分)
  3. Jmeter 压力测试笔记(3)--脚本调试/签名/cookie/提升吞吐量/降低异常率/提升单机并发性能
  4. 通过operator函数将字符串转换回运算符
  5. 数据科学 R语言速成
  6. PowerShell入门简介
  7. NullPointerException的处理新方式,Java14真的太香了
  8. Boyer-Moore字符串搜索(BM算法)的Python实现
  9. Python设计模式(10)-模板模式
  10. hadoop(十一)HDFS简介和常用命令介绍