首页 > 解决方案 > 从 mvc 视图重定向到角度路由 - Angular 6

问题描述

我已将我的 Angular 项目与 ASP.NET MVC 集成。我想在第一次加载时将我的 MVC 页面重定向到角度路由。我的 Index.cshtml 页面中有以下代码

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Myapp</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 class="skin-blue sidebar-mini wysihtml5-supported">
<app-root></app-root>
<script type="text/javascript" src="~/Scripts/client/runtime.js"></script>
<script type="text/javascript" src="~/Scripts/client/polyfills.js"></script>
<script type="text/javascript" src="~/Scripts/client/styles.js"></script>
<script type="text/javascript" src="~/Scripts/client/vendor.js"></script>
<script type="text/javascript" src="~/Scripts/client/main.js"></script>
</body>
</html>

我在角度索引页面中也有 index.html 页面配置。

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Myapp</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>
<div class="wrapper">
</div>
</app-root>
</body>
</html>

现在我需要将页面从这里重定向到 Angular 路径,其中登录路径是为 Angular 定义的。

{ path: 'login', component: LoginComponent },

标签: asp.net-mvcangularasp.net-mvc-5angular6angular-routing

解决方案


您拥有 Angular 从您的 cshtml 文件运行所需的一切。不需要 index.html 文件。

尝试将此添加到您的路线配置中。

{path:'', redirectTo:'login', pathMatch: 'full' }

添加此 Angular 后,默认情况下应重定向到登录路由。


推荐阅读