Angular 8 inside of ASP Net Core 因不允许的 MIME 类型(“text/html”)而被阻止

Angular 8 inside of ASP Net Core was blocked because of a disallowed MIME type (“text/html”)

我正在尝试设置我自己的 angular 8 应用程序,以便从 ASP.Net 核心网站内部提供服务(因此我不必处理跨站点脚本或者有两个(子)域,或者身份验证问题。

我在 VS Code 中构建了一个 Web 应用程序,并将其编译到我的 asp net core 3.0 项目中的文件夹 wwwroot/app(我可以 运行 使用 ng serve就好了)。

我已经在 Startup ConfigureServices()

中添加了这个
services.AddSpaStaticFiles(configuration =>
{
 configuration.RootPath = "wwwroot/app";
});

并在 Configure()

中加入这个
app.UseSpa(spa => {});

这部分有效。当我加载 asp.net 项目时,angular 应用程序开始加载。但是控制台充满了(生成的index.html中指定的每个文件一个):

Loading module from “https://localhost:5001/runtime.js” was blocked because of a disallowed MIME type (“text/html”).

Loading module from “https://localhost:5001/polyfills.js” was blocked because of a disallowed MIME type (“text/html”).

Loading module from “https://localhost:5001/vendor.js” was blocked because of a disallowed MIME type (“text/html”).

Loading module from “https://localhost:5001/styles.js” was blocked because of a disallowed MIME type (“text/html”).

Loading module from “https://localhost:5001/main.js” was blocked because of a disallowed MIME type (“text/html”).

据我了解,angular-cli 在文件 "compiled" 时不包含 mime 类型,因此网络浏览器不关心它们。

angular 人认为这不是 error/bug 并且已经关闭了与此相关的问题(尽管此行为与 Angular 7 的做法不同): https://github.com/angular/angular-cli/issues/10325

阅读这个问题我想我也许可以做这样的事情:

var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".js"] = "application/javascript";
provider.Mappings[".html"] = "text/html";
provider.Mappings[".ico"] = "image/vnd.microsoft.icon";


var statFilesOptions = new StaticFileOptions
{
    ContentTypeProvider = provider
};

app.UseSpa(spa =>
{
    spa.Options.DefaultPageStaticFileOptions = statFilesOptions;
});

将 mime/types 添加到文件中。但这根本没有任何作用。

关于如何解决此问题的任何其他想法无需编辑生成的 文件?

问题完全出乎我的意料。 asp.net 核心应用程序实际上为所有请求返回了 index.html 文件,因为它找不到 javascript 文件。因此,错误实际上是正确的。

index.html 中的 <base href="/"> 更改为 <base href="/app/"> 部分解决了这个问题,因为它现在知道在我放置它们的应用程序文件夹中查找文件,但重新加载停止工作(因为 url 是错误的)。

我的解决方案是将 angular 构建的输出文件夹设置为 asp net core 项目中 /wwwroot/ 的根目录。现在一切正常了。