博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET Core 找不到 npm指令异常
阅读量:7074 次
发布时间:2019-06-28

本文共 2538 字,大约阅读时间需要 8 分钟。

1.错误再现

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

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

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

 

2.相关资料

找到以下资料

 

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            }        }    }}
View Code

在Main方法中调用

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

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

你可能感兴趣的文章
[转]优化数据库大幅度提高Oracle的性能
查看>>
openwrt-智能路由器hack技术(1)---"DNS劫持"
查看>>
第十二章 数据备份与还原
查看>>
[redis] Redis 配置文件置参数详解
查看>>
Java 多线程程序设计
查看>>
浅谈TypeScript
查看>>
REST API出错响应的设计(转)
查看>>
js弹出层学习
查看>>
Oracle配置和使用闪回
查看>>
thinkphp中的AJAX返回ajaxReturn()
查看>>
BZOJ4347 : [POI2016]Nim z utrudnieniem
查看>>
jquery validate自定义checkbox验证规则和样式
查看>>
WPF自定义控件与样式(14)-轻量MVVM模式实践
查看>>
EF Code First学习系列
查看>>
Memcache服务器端参数说明
查看>>
SQLServer 复制和数据库镜像 具体配置部署
查看>>
ASP.NET MVC Model绑定的简单应用
查看>>
长期演进技术(LTE,Long Term Evolution)
查看>>
数学之路-python计算实战(5)-初识numpy以及pypy下执行numpy
查看>>
SQL--类型转换
查看>>