C# 映射SPA asp.net核心外部的特定路线
我正在开发一个SPA asp.net core-3应用程序。我试着有两条路线:C# 映射SPA asp.net核心外部的特定路线,c#,asp.net,asp.net-core,C#,Asp.net,Asp.net Core,我正在开发一个SPA asp.net core-3应用程序。我试着有两条路线: /simulate -> This routes outside SPA, to a basic controller that produces raw html /* -> any other routes will route to the angular SPA 我觉得我在启动配置中使用MapWhen时已经涵盖了它,但是当我浏览到localhost:5000/simulate时,我
/simulate -> This routes outside SPA, to a basic controller that produces raw html
/* -> any other routes will route to the angular SPA
我觉得我在启动配置中使用MapWhen
时已经涵盖了它,但是当我浏览到localhost:5000/simulate
时,我得到localhost没有发送任何数据。错误\u空\u响应
。SPA仍按预期工作,但simulate
路线似乎被忽略。任何帮助都将不胜感激
模拟控制器.cs
namespace Charla.Controllers {
public class SimulateController : ControllerBase {
[HttpGet]
public ContentResult Index()
{
return base.Content("<html><body><div>Hello</div></body></html>", "text/html");
}
}
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
{
...
app.UseRouting();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/hub");
});
app.MapWhen(
ctx => !ctx.Request.Path.StartsWithSegments("/simulate"),
appBuilder => {
app.UseSpa(spa => {
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment()) {
//spa.UseAngularCliServer(npmScript: "start");
spa.UseProxyToSpaDevelopmentServer("http://localhost:5555");
}
});
});
}
namespace Charla.Controllers{
公共类模拟器控制器:ControllerBase{
[HttpGet]
公共内容结果索引()
{
返回base.Content(“Hello”、“text/html”);
}
}
}
Startup.cs
namespace Charla.Controllers {
public class SimulateController : ControllerBase {
[HttpGet]
public ContentResult Index()
{
return base.Content("<html><body><div>Hello</div></body></html>", "text/html");
}
}
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
{
...
app.UseRouting();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/hub");
});
app.MapWhen(
ctx => !ctx.Request.Path.StartsWithSegments("/simulate"),
appBuilder => {
app.UseSpa(spa => {
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment()) {
//spa.UseAngularCliServer(npmScript: "start");
spa.UseProxyToSpaDevelopmentServer("http://localhost:5555");
}
});
});
}
public void配置(IApplicationBuilder应用程序、iWebHostenEnvironment环境、IServiceProvider服务提供商)
{
...
app.UseRouting();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller}/{action=Index}/{id?}”);
});
app.UseEndpoints(端点=>
{
endpoints.MapHub(“/hub”);
});
app.MapWhen(
ctx=>!ctx.Request.Path.StartsWithSegments(“/simulate”),
appBuilder=>{
app.UseSpa(spa=>{
//要从ASP.NET Core了解更多有关提供角度SPA的选项,
//看https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath=“ClientApp”;
if(env.IsDevelopment()){
//spa.UseAngularCliServer(npmScript:“启动”);
spa.UseProxyToSpadDevelopmentServer(“http://localhost:5555");
}
});
});
}
您是否尝试向控制器添加路由
属性
[Route("[controller]")]
public class SimulateController : ControllerBase {
[HttpGet]
public ContentResult Index()
{
return base.Content("<html><body><div>Hello</div></body></html>", "text/html");
}
}
[路由(“[控制器]”)]
公共类模拟器控制器:ControllerBase{
[HttpGet]
公共内容结果索引()
{
返回base.Content(“Hello”、“text/html”);
}
}
将MapController添加到端点而不是UseMvc
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
endpoints.MapHub<ChatHub>("/hub");
});
app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
//spa.UseAngularCliServer(npmScript: "start");
spa.UseProxyToSpaDevelopmentServer("http://localhost:5555");
}
});
app.UseRouting();
app.UseEndpoints(端点=>
{
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{controller}/{action=Index}/{id?}”);
endpoints.MapHub(“/hub”);
});
app.UseSpa(spa=>
{
//要从ASP.NET Core了解更多有关提供角度SPA的选项,
//看https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath=“ClientApp”;
if(env.IsDevelopment())
{
//spa.UseAngularCliServer(npmScript:“启动”);
spa.UseProxyToSpadDevelopmentServer(“http://localhost:5555");
}
});
你的app.MapWhen
应该放在app.UseMvc
之前。李玉高尝试了这个方法,但没有运气移动UseMvc并使用端点