简体   繁体   English

如何重定向到SPA中的路由?

[英]How to redirect to a route in SPA?

I have authentication done using external provider in Ids3. 我已经使用Ids3中的外部提供程序完成了身份验证。 After the user authenticates, my MVC home page is loaded, which bootstraps the angular app as such: 在用户身份验证之后,将加载我的MVC主页,该主页将这样引导角度应用程序:

@section AppScripts {
    @Scripts.Render("~/bundles/Swagger")
    @Scripts.Render("~/bundles/DevPortalApp")
}

When the user authenticates through one of the external providers, I am supposed to redirect to a specific page in angular: 当用户通过外部提供商之一进行身份验证时,我应该以角度重定向到特定页面:

https://myaddress/DevPortalApp/something

The problem is that with regular redirects I am stuck in the authentication loop. 问题在于,使用常规重定向时,我陷入了身份验证循环。 How can I redirect from my Home controller to a specific angular page? 如何从Home控制器重定向到特定的角度页面?

 var externalLogin = accessToken.externalLogin;

 // Check if its from external
 if (externalLogin.Value != null)
 {
     var isValid = await externalLoginService.ValidateAccessToken(accessToken);
     RedirectToAction("~/#/myURLRedirect");
 }

You need to use angular router from here , in shortly you can use $state.go('your-new-view') 您需要从这里使用角度路由器,不久您可以使用$state.go('your-new-view')

In your app.js file create the states list- 在您的app.js文件中,创建状态列表-

$stateProvider
.state('someState', {
   url: '/someState',

   templateUrl: 'templates/Swagger.html',
   controller: 'Swagger'
})

And in your home controller you can just add redirect with $state.go('someState') 并且在您的家庭控制器中,您只需使用$state.go('someState')添加重定向

There are few multiple ways to integrate ASP.Net MVC Route with Angular Route. 有几种将ASP.Net MVC Route与Angular Route集成的方法。

I personally like Miguel Castro's approach which uses *catchall in ASP.Net MVC, and then pass it to Angular. 我个人喜欢Miguel Castro的方法 ,该方法在ASP.Net MVC中使用*catchall ,然后将其传递给Angular。 Then let Angular handle at client-side. 然后让Angular在客户端处理。

You can fork my working sample code at GitHub . 您可以在GitHub上分叉我的工作示例代码。

MVC Route MVC路线

routes.MapRoute(
   name: "Users",
   url: "users/{*catchall}",
   defaults: new { controller = "Users", action = "Index" });

Angular Route 角线

$routeProvider
   .when(rootPath + "users", {
      template: "<user-list></user-list>",
      caseInsensitiveMatch: true
   })
   .otherwise({ redirectTo: (rootPath + "users") });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM