首页 > 解决方案 > 使用角度刷新 ASP .NET 中的路由时不加载 HTML

问题描述

我已经构建了我的 Angular 应用程序,输出是我的 ASP .NET API 文件夹中的 wwwroot 文件夹。当我使用 dotnet watch run 构建应用程序时,一切正常并显示,但是当我刷新路由 localhost:5000/users 时,它显示的是原始 json 文件而不是 HTML/CSS 内容

控制台错误显示以下消息:内容安全策略:页面的设置阻止在 https://localhost:5000/favicon.ico (“default-src”) 加载资源;在网络引用策略中说“严格起源时跨起源”,但我的 cors 已设置app.UseCors(x=>x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());

我的端点

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
               endpoints.MapFallbackToController("Index","Fallback");
            
                
            });

后备控制器

 public class FallbackController : Controller
    {
        public IActionResult Index(){

            return PhysicalFile(Path.Combine(Directory.GetCurrentDirectory(),"wwwroot","index.html"),
            "text/HTML");
        }
    }

来自 wwwroot 文件夹的 Index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>SocialNetworkSPA</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>

我的 ASP .NET Core 版本是 3.1 和 Angular 11

错误图片http://i.prntscr.com/EX2RFqRrSkS9b2gEESbdLQ.png

请不要在我刷新页面之前一切正常并正常工作,然后出现这些错误

标签: asp.netangularasp.net-coreasp.net-web-apibuild

解决方案


尝试这个:-

 app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().WithOrigins("https://localhost:xxxx"));

并且不要忘记添加services.AddCors();到您的 ConfigureServices。


推荐阅读