我在 Visual Studio 2015 中使用模板创建了简单的 Asp.Net 默认项目:ASP.NET Core Web 应用程序 (.NET Core)/Web 应用程序,无身份验证。
然后,我创建了 package.json,来加载 React 的所有内容,如下:
{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"devDependencies": {
"babel-cli": "6.18.0",
"webpack": "1.14.0",
"babel-core": "6.21.0",
"babel-loader": "6.2.10",
"babel-preset-react": "6.16.0",
"babel-preset-es2015": "6.18.0",
"react": "15.4.1",
"react-dom": "15.4.1"
}
}
我的webpack-config.js:
var config = {
entry: './ClientApp/main.jsx',
output: {
path: './wwwroot/js',
filename: 'index.js',
},
devServer: {
inline: true,
port: 8080
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015', 'react']
}
}
]
}
}
module.exports = config;
它工作得很好,但主要问题是我们需要独立运行服务器端的 Kestrel 和客户端的 npm。
当我从 Visual Studio 启动项目时,如何启动所有客户端(npm)脚本来创建 index.js 文件?
最佳答案
在 Startup.cs 文件中的配置方法中设置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseSpa(spa =>
{
spa.Options.SourcePath = "client";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer("start");
}
});
}
关于javascript - 结合 Asp.Net Core 服务器端和 React 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41288771/