【发布时间】:2020-05-20 01:23:44
【问题描述】:
我有一个使用CRA 创建的 ASP.NET 核心反应网站。 React 应用程序适用于只读的面向公众的页面。
例如,我还希望将一些管理页面创建为 EF 核心脚手架 Razor 页面;
- https://localhost/robots - 响应应用程序以向用户显示机器人列表
- https://localhost/robots/admin - MVC 应用程序向用户显示机器人列表和管理操作
如何配置路由以适应这两种情况?
启动配置如下;
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));
}
// 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();
}
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.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "admin",
pattern: "admin/{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
//spa.UseReactDevelopmentServer(npmScript: "start");
spa.UseProxyToSpaDevelopmentServer("http://localhost:3000");
}
});
}
【问题讨论】:
标签: asp.net-mvc reactjs asp.net-core-3.1 asp.net-core-routing