Asp.Net核心地图与MapWhen

问题描述 投票:0回答:1

在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");
    });
});

这有什么不对?

angular asp.net-core angular6
1个回答
0
投票

对于MapMapWhen,当使用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

            });
        });
© www.soinside.com 2019 - 2024. All rights reserved.