#asp.net #reactjs #.net #release #publish
Вопрос:
Не могу понять этого ни за что на свете. У меня есть простой шаблон .NET 5.0 React SPA (я ничего не изменил), и он отлично работает, когда я настраиваю его для локального запуска в режиме отладки. Но если я переключу его в режим выпуска, я получу сообщение об ошибке 500, в котором говорится, что страница в настоящее время не может обработать этот запрос.
Вот файл Program.cs.
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SpaTest {
public class Program {
public static void Main( string[] args ) {
CreateHostBuilder( args ).Build().Run();
}
public static IHostBuilder CreateHostBuilder( string[] args ) =>
Host.CreateDefaultBuilder( args )
.ConfigureWebHostDefaults( webBuilder => {
webBuilder.UseStartup<Startup>();
} );
}
}
и файл Startup.cs
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SpaTest.Data;
using SpaTest.Models;
namespace SpaTest {
public class Startup {
public Startup( IConfiguration configuration ) {
Configuration = configuration;
}
public IConfiguration Configuration {
get;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices( IServiceCollection services ) {
services.AddDbContext<ApplicationDbContext>( options =>
options.UseSqlServer(
Configuration.GetConnectionString( "DefaultConnection" ) ) );
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDefaultIdentity<ApplicationUser>( options => options.SignIn.RequireConfirmedAccount = true )
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddControllersWithViews();
services.AddRazorPages();
// In production, the React files will be served from this directory
services.AddSpaStaticFiles( configuration => {
configuration.RootPath = "ClientApp/build";
} );
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure( IApplicationBuilder app, IWebHostEnvironment env ) {
if ( env.IsDevelopment() ) {
app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
} else {
app.UseExceptionHandler( "/Error" );
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseIdentityServer();
app.UseAuthorization();
app.UseEndpoints( endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}" );
endpoints.MapRazorPages();
} );
app.UseSpa( spa => {
spa.Options.SourcePath = "ClientApp";
if ( env.IsDevelopment() ) {
spa.UseReactDevelopmentServer( npmScript: "start" );
}
} );
}
}
}
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=aspnet-SpaTest-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"IdentityServer": {
"Clients": {
"SpaTest": {
"Profile": "IdentityServerSPA"
}
}
},
"AllowedHosts": "*"
}
Мысли?
Комментарии:
1. Не могли бы вы также показать нам свое
.csproj
досье?