如何配置 ASP.NET 酷睿3.1应用程序以并行运行ReactJS和MVC网站



我有一个用CRA创建的 ASP.NET 核心反应网站。反应应用程序适用于面向公众的只读页面。

例如,我还想将一些管理页面创建为 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");
}
});
}

startup.cs终结点映射是为 MVC 配置的。这可以工作,但改变它会更容易;

改变

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "admin",
pattern: "admin/{controller}/{action=Index}/{id?}");
});

app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});

从 https://andrewlock.net/comparing-startup-between-the-asp-net-core-3-templates/和 https://www.learnrazorpages.com/razor-pages/routing 中了解到的信息

最新更新