asp.net - 使用角度刷新 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
请不要在我刷新页面之前一切正常并正常工作,然后出现这些错误
解决方案
尝试这个:-
app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().WithOrigins("https://localhost:xxxx"));
并且不要忘记添加services.AddCors();
到您的 ConfigureServices。
推荐阅读
- sqlalchemy - Query all fields when using single table inheritance
- python - 如何删除其他 python 版本并继续使用 python 3.5
- r - 使用 dplyr 迭代 t 检验
- javascript - 当 Accordion SelectedIndexChanged 时从动态创建的 Accordion 面板获取 Pane.ID
- google-app-engine - Appengine - 上传文件时出现 tmp/文件系统错误
- css - 如何阻止(但不阻止!)浏览器在元素中自动换行?
- c++ - 创建部分屏幕外的 ncurses 窗口
- r - 交叉表有两个以上组时的 Dplyr 计算
- r - 在 R 中按比例分组
- graphdb - GraphDB 在事务中读取、检查和更新