我需要在错误的 api 调用上返回 404,以便我可以在客户端(Angular 5)上为用户创建正确的响应。目前后端返回状态码200和index.html,这会导致前端出现json解析错误。
我使用 ASP.NET Core 2.1 和 Angular 模板项目创建了新的 Web 项目。这是the link https://wrongapiurltestapp.azurewebsites.net/到该模板的已发布版本。我根本没有更改模板代码,我只是按原样将其发布在 azure 上。 (在开发模式下,我如预期收到 404 错误)。
如上所述here https://github.com/aspnet/JavaScriptServices/issues/973#issuecomment-303778580默认行为应该是 404。
Here https://pastebin.com/Wme9MKUR is the Startup.cs
, and here https://pastebin.com/wtGXkH9Z SampleDataController.cs
.
我想在中使用类似的东西Startup.cs
:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}");
});
app.MapWhen(x => x.Request.Path.Value.Contains("/api"), builder =>
{
builder.UseMvc(routes =>
{
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Error" });
});
});
And in HomeController.cs
:
public IActionResult Error()
{
return NotFound();
}
但这似乎是错误的,尽管它有效。