Angular 9 和 dotnet WebAPI 路由
Angular 9 and dotnet WebAPI routing
我使用 Visual Studio 模板创建了一个带有 Web API (.net core 3.1) 的 Angular SPA,它已部署到 Azure App Service。 SPA 可以很好地调用 API 个端点。
但是,当我尝试从移动客户端调用 API 时
api/v1/users/123
用户 ID 123 不存在,我希望是 404,但似乎 Angular 路由接管了它 returns SPA 的主页。
是否有一些我可以做的配置可以解决这个问题,或者我是否以错误的方式解决了这个问题?
此外,我在应用程序服务中添加了两个子域,一个用于 SPA,一个用于 API:portal.NN.com 和 api.NN.com。使用 api.NN.com 时是否可以禁用 SPA?
编辑澄清:WebAPI 由 SPA 和 Xamarin 移动应用程序使用。我的 Xamarin 应用程序中的问题是,当我尝试查找不存在的资源时,我没有得到 404,或者如果路由不存在,它会重定向到 SPA 主页(可以看到 HTML 时我反序列化 JSON).
编辑 #2 - 从 Startup.cs
配置
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseMiddleware<ErrorHandlingMiddleware>();
app.UseCors("CorsPolicy");
if (!env.IsDevelopment())
{
// 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();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
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.UseProxyToSpaDevelopmentServer("http://localhost:4200");
}
});
}
尝试使用 app.UseWhen 功能。
app.UseWhen(x=> !x.Request.Path.Value.Contains("ApiEndPoint"), app1 =>
app1.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
})
);
希望这会有所帮助。
我使用 Visual Studio 模板创建了一个带有 Web API (.net core 3.1) 的 Angular SPA,它已部署到 Azure App Service。 SPA 可以很好地调用 API 个端点。
但是,当我尝试从移动客户端调用 API 时
api/v1/users/123
用户 ID 123 不存在,我希望是 404,但似乎 Angular 路由接管了它 returns SPA 的主页。
是否有一些我可以做的配置可以解决这个问题,或者我是否以错误的方式解决了这个问题?
此外,我在应用程序服务中添加了两个子域,一个用于 SPA,一个用于 API:portal.NN.com 和 api.NN.com。使用 api.NN.com 时是否可以禁用 SPA?
编辑澄清:WebAPI 由 SPA 和 Xamarin 移动应用程序使用。我的 Xamarin 应用程序中的问题是,当我尝试查找不存在的资源时,我没有得到 404,或者如果路由不存在,它会重定向到 SPA 主页(可以看到 HTML 时我反序列化 JSON).
编辑 #2 - 从 Startup.cs
配置public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseMiddleware<ErrorHandlingMiddleware>();
app.UseCors("CorsPolicy");
if (!env.IsDevelopment())
{
// 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();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
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.UseProxyToSpaDevelopmentServer("http://localhost:4200");
}
});
}
尝试使用 app.UseWhen 功能。
app.UseWhen(x=> !x.Request.Path.Value.Contains("ApiEndPoint"), app1 =>
app1.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
})
);
希望这会有所帮助。