c# - 与 Typescript 热重载 IIS 网络服务器 react

标签 c# asp.net reactjs typescript iis

在使用 C# Web Api 后端使用 TypeScript 开发我的 React 应用程序时,我想使用热重载。我使用 .Net 框架而不是 Core,所以我需要使用 IIS 或 IIS Express。我可以使用 webpack dev server 为前端热重载而不会出现问题,但我无法访问 API 资源。我能做到吗?

最佳答案

使用 webpack dev server 找到解决方案 reverse proxy到 IIS。

新公共(public)管理:

npm install --save react-hot-loader@next

npm install webpack-dev-server --save-dev

webpack.config.js,proxy是IIS运行的地方:

var webpack = require('webpack');
var path = require("path");
var proxy = 'localhost:61299';

module.exports = {
    entry: [
        // activate HMR for React
        'react-hot-loader/patch',

        // the entry point of our app
        './Scripts/src/index.tsx',
    ],
    //entry: "./Scripts/src/index.tsx",
    output: {
        filename: "./Scripts/dist/bundle.js",
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },

    module: {
        loaders: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
            //{ test: /\.tsx?$/, loader: "ts-loader" }
            { test: /\.tsx?$/, loader: ['react-hot-loader/webpack', 'ts-loader'] }
        ]
    },

    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        // enable HMR globally

        new webpack.NamedModulesPlugin(),
        // prints more readable module names in the browser console on HMR updates
    ],

    devServer: {
        proxy: {
            '*': {
                target: 'http://' + proxy,
            }
        },
        port: 8080,
        host: '0.0.0.0',
        hot: true,
    },
}

然后我可以从项目根文件夹使用此命令启动 Web 服务器:node_modules\.bin\webpack-dev-server。如果我随后访问 http://localhost:8080/ 我有热重载,我仍然可以使用 C# web api,因为它会在“http://localhost:61299/”代理 IIS Express

关于c# - 与 Typescript 热重载 IIS 网络服务器 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43544077/

相关文章:

c# - 创建一个可以使用 C# 中的循环更改其名称的矩形

jquery - 如何在客户端验证 google recaptcha?

c# - 在 ASP.NET 中连接到 SQL Server 数据库

c# - 如何将两个列值添加到列表

javascript - Immutable js - Redux-Saga 按键查找项目并更新值

javascript - React-Modal 关闭按钮不起作用

javascript - 扩展 TextInput 以在 React Native 中自定义默认字体和文本颜色,就像在 Java 中一样

c# - 避免在主窗体顶部使用全局变量的良好设计模式是什么?

c# - 制作一些 OnIdiom 和 OnPlatform StaticResource

c# - 如何设置附加到 Azure 存储的 Azure 搜索软删除策略