在Startup.cs中,我有工作代码:
app.Map("/ca", ca =>
{
ca.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApps/AngularApp";
spa.UseAngularCliServer(npmScript: "start");
});
});
我认为以下是平等的,但它不起作用:
app.MapWhen(ctx => ctx.Request.Path.StartsWithSegments("/ca"), ca =>
{
ca.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApps/AngularApp";
spa.UseAngularCliServer(npmScript: "start");
});
});
这有什么不对?
对于Map
和MapWhen
,当使用Map时,匹配的路径段将从HttpRequest.Path中删除,并附加到HttpRequest.PathBase以获取每个请求。当使用MapWhen
时,它没有。
对于Spa
,它将使用requestPathBase: context.Request.PathBase.ToString());
,检查SpaPrerenderingExtensions。
要获得相同的结果,您可以尝试自行删除和追加Path。
app.MapWhen(ctx => {
if (ctx.Request.Path.StartsWithSegments("/ca"))
{
ctx.Request.Path = ctx.Request.Path.Value.Replace("/ca","");
ctx.Request.PathBase = "/ca/";
return true;
}
return false;
}, ca =>
{
ca.UseSpa(spa =>
{
//rest code
});
});