0

请帮忙。我有一个从 VS2017 启动时完美运行的反应应用程序。托管在 Azure VM(IIS-8、Windows Server)上的同一个应用程序给我 404 或 500 错误。

我的托管目录结构适用于我的 .Net 应用程序以及混合的 .net 和 React 应用程序,但不适用于我的新的仅 React 应用程序。

我的目录结构是 wwwroot>Dashboard。我将我的生产版本复制到此仪表板文件夹。

我的网络配置是:

`<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <location path="." inheritInChildApplications="false">
          <system.webServer>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\Dashboard.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
            <rewrite>
                    <rules>
                        <rule name="React Routes" stopProcessing="true">
                            <match url=".*" />
                                <conditions logicalGrouping="MatchAll">
                                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                                    <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
                                </conditions>
                            <action type="Rewrite" url="/" />
                        </rule>
                    </rules>
                </rewrite>
            </system.webServer>
        </location>
    </configuration>`

在此处输入图像描述

4

1 回答 1

0

As far as I know, the asp.net core application use services.AddSpaStaticFiles to serve React application instead of using url rewrite.

I suggest you could check the Startup.cs's Configure and ConfigureServices method to make sure you have added the SPA settings.

Codes as below:

     public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // In production, the React files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/build";
        });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            //the source path of the react application
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });
    }
于 2019-04-04T02:46:07.297 回答