无法启动 net 6 + React 应用程序的 SPA 代理

2024-02-20

我正在创建一个基于 ASP.NET CORE Web 应用程序 + React 模板的新项目。然后我根据需要配置 ClientApp - 我使用自己的 webpack.config.js,而不是 CRA。前端工作正常,当我运行它时它按预期启动npm start。但后端无法启动。问题来自 SPA 代理,它无法正常工作。当我启动应用程序时,我收到以下消息:SPA proxy is not ready. Returning temporary landing page。同时,前端页面可以正确打开,但如果没有代理服务器,应用程序基本上无法运行。我缺少什么想法吗?

我的 package.json 文件的内容:

{
  "name": "project1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --open --mode development --hot",
    "build": "webpack --mode production"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.17.9",
    "@babel/preset-env": "^7.16.11",
    "@babel/preset-react": "^7.16.7",
    "babel-loader": "^8.2.4",
    "css-loader": "^6.7.1",
    "html-webpack-plugin": "^5.5.0",
    "mini-css-extract-plugin": "^2.6.0",
    "node-sass": "^7.0.1",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "sass": "^1.51.0",
    "sass-loader": "^12.6.0",
    "style-loader": "^3.3.1",
    "ts-loader": "^9.2.8",
    "webpack": "^5.72.0",
    "webpack-cli": "^4.9.2",
    "webpack-dev-server": "^4.8.1"
  },
  "dependencies": {
    "@fluentui/react": "^8.65.0",
    "@types/react": "^18.0.5",
    "@types/react-dom": "^18.0.0",
    "typescript": "^4.6.3"
  }
}

.csproj 文件的内容:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
    <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
    <SpaProxyServerUrl>http://localhost:5206</SpaProxyServerUrl>
    <SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="6.0.6" />
  </ItemGroup>

  <ItemGroup>
    <!-- Don't publish the SPA source files, but do show them in the project files list -->
    <Content Remove="$(SpaRoot)**" />
    <None Remove="$(SpaRoot)**" />
    <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
  </ItemGroup>

  <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  </Target>

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)build\**" />
      <ResolvedFileToPubliFsh Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>wwwroot\%(RecursiveDir)%(FileName)%(Extension)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPubliFsh>
    </ItemGroup>
  </Target>
</Project>

webpack.config.js 文件:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpackDevServerPort = 8083;
const proxyTarget = "http://localhost:5206";

module.exports = {
    mode: 'development',
    entry: './index.tsx',
    devtool: 'inline-source-map',
    output: {
        path: path.join(__dirname, '/dist'),
        filename: 'bundle.js'
    },
    devtool: 'inline-source-map',
    devServer: {
        static: './dist',
        proxy: {
            '*': {
                target: proxyTarget
            }
        },
        port: webpackDevServerPort
    },
    module: {
        rules: [
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
            },
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            },
            {
                test: /\.(s*)css$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'sass-loader',
                ]
            }
        ]
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
    },
    plugins:[
        new HtmlWebpackPlugin({
        template: './index.html'
        })
    ]
}

启动设置.json:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:45536",
      "sslPort": 44368
    }
  },
  "profiles": {
    "Project1": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5206",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
      }
    }
  }
}

  1. 打开你的ClientApp目录。

  2. 在终端中运行以下命令:

npm 我 spa 代理


如果上述解决方案失败,请尝试以下操作:

  1. 打开你的ClientApp目录。

  2. 在终端中运行以下命令:

npm 更新

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法启动 net 6 + React 应用程序的 SPA 代理 的相关文章

随机推荐

  • SceneKit - 向场景添加新的 SCNNode 会导致严重的滞后

    我发现向场景中添加 SCNNode 使用 SCNGeometry 会导致严重的滞后峰值 根据时间分析器 它必须生成几何图形 至少函数 方法是这样调用的 它是在节点添加到场景时执行的 而不是在创建节点时执行的 因此 使用 SCNNode 创建
  • 保存下载但不带文件引用

    无论如何 是否可以使用 URLLoader 下载文件 然后将其保存到磁盘而不使用文件引用或任何使用对话框的内容 这是我拥有但不起作用的 public function onDownloadComplete e Event void Down
  • Golang 中 logrus 的 CustomFormatter 显示文件名和行号

    我在用github com sirupsen logrus用于登录我的 golang 脚本 但是我想获取记录消息的文件名和行号 我可以使用下面的代码得到它 package main import fmt os runtime strings
  • Monodevelop 2.8、XCode 3.2.6、界面生成器:出口和操作

    编辑 由于我还没有安装 XCode 4 我想知道 MD 2 8 是否与 XCode 3 2 6 完全兼容 特别是 我是否能够将插座和操作与中描述的新过程连接起来机器翻译文档 http docs xamarin com ios tutoria
  • 如何在 swift 3+ 中调整键盘的滚动视图

    如何调整滚动视图以垂直补偿键盘 继续阅读 是的 我知道这是一些基本信息 但今天我随机注意到 我看到的关于这个主题的所有答案都充满了信息 版本和 或到处使用刘海 但对于 Swift 来说没有什么可靠的3 斯威夫特 4 2 代替滚动视图对于 U
  • Android ExpandableListView:单击时设置所选项目的背景颜色

    当用户单击我的子项目时 我试图为项目设置背景颜色expandableListView 这是代码 expListView setOnChildClickListener new OnChildClickListener Override pu
  • 在 FluentValidation 中覆盖默认 ASP.NET MVC 消息

    我收到验证消息 值 xxx 对于 yyy 无效 当我为双精度类型发布错误的值时 就会发生这种情况 我不知道如何改变它 不幸的是 FluentValidation 无法覆盖这一点 MVC 验证的可扩展性模型在许多地方都受到一定限制 而且我无法
  • iOS - 通过渲染从 UIView 生成 PDF 会降低质量

    我使用以下方法从 UIView 生成 PDF 它们都创建了 PDF 但质量下降了 方法一 implementation UIView PDFWritingAdditions void renderInPDFFile NSString pat
  • 如何在validationif装饰器nestjs类验证器中使用else条件?

    我需要有条件地验证在 Nestjs 类验证器中提交的输入 有一个 validateif 装饰器 但如何在 else 部分添加另一个验证 例如 如果第一个输入是电子邮件 则使用电子邮件装饰器 如果它是电话 则与我的正则表达式匹配 IsNotE
  • DCF77 解码器与噪声信号

    我几乎完成了我的开源 DCF77 解码器项目 当我注意到标准 Arduino DCF77 库在噪声信号上表现非常差时 这一切就开始了 特别是当天线靠近计算机或洗衣机正在运行时 我永远无法从解码器中获取时间 我的第一个方法是向输入信号添加 数
  • JavaFx:组合框表格单元格双击

    问题如下 我有一个TableView with ComboBoxes对于每个 TableCell 我可以选择 组合框中的值 问题是 如果我有很多行和列 我必须多次单击才能在每个组合框中选择适当的值 要在组合框中选择一个值 我必须单击四次才能
  • JDK5/JDK 6 将 String 转换为 Date 时有什么区别

    JDK 5 是否可以生成 default message Failed to convert property value of type java lang String to required type java util Date f
  • VIEW 中的 ORDER BY 返回不同结果的 SQL

    这是我的观点 CREATE VIEW STD USER view TransInvoice AS SELECT TOP 999999 Customernr Referensnr 2 as a InvoiceRowData FileHead
  • wxPython 因分段错误而崩溃

    我很困惑为什么我的应用程序因分段错误而崩溃 我有一个使用 wxPython 作为前端的 python 应用程序 我的应用程序因分段错误而随机崩溃 我知道它必须是 wxPython 因为我有相同代码的控制台版本 并且它不会崩溃 前端是一个只读
  • Docker 容器中卷的权限错误

    我通过本地 docker machine VM 在 OSX 10 11 中运行 Docker 1 8 1 我有以下 docker compose yml web build docker web ports 80 80 8080 8080
  • 在 Monotouch 中编译 lambda 并调用设备上的委托

    我目前正在 MonoTouch 中移植 NET 代码库 并且目前正在研究一种接收Expression
  • 为什么我的“计数前导零”程序出现故障?

    以下代码返回 Hacker s Delight 书中的前导零的数量 include
  • 当我重命名项目时,Xcode 6.3 崩溃

    昨天我将 Xcode 更新到 6 3 版本 因为我的 iPhone 具有 8 3 软件版本 并且 Xcode 想要最新版本来运行该应用程序 现在一切都已更新 但 Xcode 无法再重命名该项目了 如果我复制该项目 然后打开它并在 身份 和
  • 如何将我的编译器与 eclipse 集成?

    我有一个使用 flex bison 和 C 编写的迷你编译器 我想在 eclipse 中使用它 就像使用java编译器一样 但我不知道如何做到这一点 所以我需要一些帮助和一些建议 Thanks 创建商业品质的 Eclipse IDE htt
  • 无法启动 net 6 + React 应用程序的 SPA 代理

    我正在创建一个基于 ASP NET CORE Web 应用程序 React 模板的新项目 然后我根据需要配置 ClientApp 我使用自己的 webpack config js 而不是 CRA 前端工作正常 当我运行它时它按预期启动npm