简体   繁体   English

ASP.NET Core - 从不同端口同时服务 API 和 SPA React

[英]ASP.NET Core - Serve both API and SPA React from different port

It is possible to serve both an API and a Single Page Application (SPA) React/Redux from different ports in ASP.NET Core.可以从 ASP.NET Core 的不同端口同时为 API 和单页应用程序 (SPA) React/Redux 提供服务。 I found some post here ASP.NET Core - Serve both API and SPA from the same port that asks for serving from the same port, I would like to serve them on different ports because I would like to implement Azure B2C Authentication(so my spa can safely communicate with web api).我在这里找到了一些帖子ASP.NET Core - 从同一端口为 API 和 SPA提供服务,要求从同一端口提供服务,我想在不同的端口上为它们提供服务,因为我想实施 Azure B2C 身份验证(这样我的水疗中心可以安全地与 web api 通信)。 I generated a boiler plate that includes react spa with .net web api but it is in one solution by default.我生成了一个样板,其中包含带有 .net web api 的 react spa,但默认情况下它在一个解决方案中。 Not sure if I should generate .net core web api and react app in a separate projects but when I run the application it serves both spa and webapi on the port that is configured in launchSettings.json for web api. In Startup.cs i have不确定我是否应该生成 .net 核心 web api 并在单独的项目中反应应用程序但是当我运行该应用程序时,它在 launchSettings.json 中配置的端口上为 web 88290284.In135988 提供 spa 和 webapi

 services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/build";
        });

  app.UseSpaStaticFiles();

  app.UseSpa(spa =>
        {
            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });

I tried to add "homepage": "https://localhost:3000", to package.json of react.app I also tried to add spa.Options.DefaultPage = "https://localhost:3000" I also tried spa.UseProxyToSpaDevelopmentServer("https://localhost:3000");我尝试将"homepage": "https://localhost:3000",添加到 react.app 的 package.json 我还尝试添加spa.Options.DefaultPage = "https://localhost:3000"我也尝试spa.UseProxyToSpaDevelopmentServer("https://localhost:3000"); But none of that works.但这些都不起作用。 Is there something like a Nuget package or maybe some extra trick with configuration or should I give up on the boiler plate and create 2 independent projects(1 create-react-app and then generate a default web api project).是否有类似 Nuget package 的东西,或者可能有一些额外的配置技巧,或者我应该放弃样板并创建 2 个独立的项目(1 个 create-react-app,然后生成默认的 web api 项目)。 Thanks谢谢

Different from the template for ASP.NET Core with React.js, React.js and Redux is using .net core3.1 so it doesn't have.env file.与 ASP.NET Core with React.js 的模板不同,React.js 和 Redux 使用的是 .net core3.1,因此它没有 .env 文件。 In that file you can set the running port of your project.在该文件中,您可以设置项目的运行端口。
According to the MS document , I think React.js and Redux project is using middleware to compile the functions code first then put them to the static web page and display it.根据MS文档,我认为React.js和Redux项目是使用中间件先编译功能代码,然后将它们放到static web页面并显示。 The following code do this part of job:以下代码完成这部分工作:

app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseSpa(spa =>
{
    spa.Options.SourcePath = "ClientApp";

    if (env.IsDevelopment())
    {
        spa.UseReactDevelopmentServer(npmScript: "start");
    }
});

When you start the project in VS, you can see the port number in the browser, which is the running port of the whole project.当你在VS中启动项目时,在浏览器中可以看到端口号,这就是整个项目的运行端口。 So you think the API and SPA are running on the same port.所以你认为 API 和 SPA 在同一个端口上运行。 From my perspective, API is using middleware to invoke SPA.在我看来,API 正在使用中间件来调用 SPA。

You can use command line code npm start in the ClientApp File to run the SPA as an independent project, it will run it on the default port 3000. Here is the screenshot of my test:您可以在 ClientApp 文件中使用命令行代码npm 启动将 SPA 作为独立项目运行,它将在默认端口 3000 上运行。这是我的测试截图:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET Core MVC 中从 Azure blob 容器提供 static 个文件 - Serve static files from Azure blob container in ASP.NET Core MVC ASP.NET 核心 6 Web API 与 firebase 身份验证和反应前端承载身份验证失败 - ASP.NET Core 6 Web API with firebase auth and react front end bearer auth fails 无法从逻辑应用执行 HTTP 后操作到 Asp.net 核心 Web API - Can't perform HTTP Post Action from Logic App to Asp.net Core Web API 从 ASP.NET Core Web API Endpoint 上传大文件到 Amazon S3 - Upload a large file to Amazon S3 from ASP.NET Core Web API Endpoint 哪个端口是我在 AWS Fargate 中运行的 ASP.NET Core 6 应用程序 - Which port is my ASP.NET Core 6 app running on in AWS Fargate 在 ASP.NET Core MVC 中从 Cognito 注销时出错 - Error logging out from Cognito in ASP.NET Core MVC asp.net 核心 web api (.net 6) 使用 startup.cs 而不是 program.cs 创建项目 - asp.net core web api (.net 6) created project with startup.cs instead of program.cs 从ASP.NET Core访问Azure App Service ConnectionString - Access Azure App Service ConnectionString from ASP.NET Core 提供来自 Google Cloud Storage 的 static SPA 和来自 Google App Engine 的 API - Serve static SPA from Google Cloud Storage and API from Google App Engine Asp.net 核心 Web API 使用 Dapper 执行 Postgres function 引发“读取超时”和“读取流时异常” - Asp.net Core Web API use Dapper to Execute Postgres function raise "Timeout during reading" and "Exception while reading from stream"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM