Search code examples
asp.net-mvcangularasp.net-mvc-5angular6angular-routing

Redirect from mvc view to angular route - Angular 6


I have integrated my angular project with ASP.NET MVC. I want to redirect my MVC page to angular route on first load. I have following code in my Index.cshtml page

<!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>

I also have the index.html page configurations in angular index page.

<!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>

Now I need to redirect the page from here to angular route where login route is defined for angular.

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


Solution

  • You have everything needed for Angular to run from your cshtml file. No need for index.html file.

    Try to add this to your route config.

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

    Once you add this Angular should redirect to login route by default.