.net - Angular 路由器发送 GET 请求而不是加载组件
问题描述
当我导航到 /exercises 页面时,我得到一个空白页面,显示 GetAllExercises 方法的结果,而不是 ExerciseListComponent。但是,如果我在 RouterModule 中将路径更改为“锻炼”,它将不再与控制器匹配并正确加载锻炼列表组件。为什么直接访问控制器而不是加载组件?
RouterModule.forRoot([
{ path: 'exercises', component: ExerciseListComponent, pathMatch: 'full' },
{ path: 'workout-plans', component: WorkoutPlanListComponent, pathMatch: 'full' },
{ path: 'workout-sessions', component: WorkoutSessionListComponent, pathMatch: 'full' },
])
[HttpGet]
[Route("exercises")]
public JsonResult GetAllExercises()
{
var context = new WorkoutTrackerContext();
var exercises = context.Exercises.ToList();
return Json(exercises);
}
解决方案
我想我明白了。它与 . 无关RouterModule
。甚至是Angular。这出戏的唯一演员是浏览器和后端。
我猜您的后端会将所有未知路由重定向到index.html
引导 Angular 应用程序的路径 - 但/exercises
它是已知路由。因此是 JSON,而不是组件 - 实际上是 JSON 而不是 Angular 应用程序及其对路由的解释。
'/'
一种解决方案是使用比基本 href更具体的东西(不知道,/app
对于 Angular,/service
对于 API?)。
推荐阅读
- android - 了解 EGLSurface、EGL、Surface 和 SurfaceView 如何相互关联
- java - 未找到类 Def,log4j2 中的渲染器映射
- mongodb - 如何查看地理距离
- typescript - 从 Typescript 中的 Promise 推断返回值
- javascript - 如何将 Chart.js 轴最小值设置为 0?
- django - 如何使用自动数据库路由为使用 PostgreSQL 的 Django 应用程序创建写入和读取副本
- sql - Oracle sql中从出生日期开始的年龄范围
- javascript - 是否有可能在进行操作后或在我的情况下在日历中创建事件然后页面使用 JavaScript 自动向下滚动?
- c# - 过滤大量收藏的最佳方法?
- django - 使用 Django Restframework 实现通知