简体   繁体   English

RedirectToAction无法与asp.net核心中的angular2一起使用

[英]RedirectToAction not working with angular2 in asp.net core

I am using Angular 2 with asp.net core.Here is the routing setup in asp.net core 我正在将Angular 2与asp.net核心一起使用。这是asp.net核心中的路由设置

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

                routes.MapRoute(
                    name: "spa-fallback",
                    template: "{*url}",defaults: new { controller = "Home", action = "Index" });
            });

Here is action method. 这是动作方法。 The action method is call from angular 2 http post 动作方法是从Angular 2 http post调用

        [HttpPost]
        public JsonResult Authentication([FromBody] UserViewModel model)
        {
            GenericResponseObject<JwtTokenViewModel> genericResponseObject = new GenericResponseObject<JwtTokenViewModel>();
            genericResponseObject.IsSuccess = false;
            genericResponseObject.Message = ConstaintStingValue.Tag_ConnectionFailed;
             AccountRepo accountRepo = new AccountRepo(_baseUrl, _memoryCache);
                if (accountRepo.JwtTokenGenerator(model) != null)
                {
                    UserViewModel userViewModel = accountRepo.UserAuthentication(model);
                    RedirectToLocal();
                }

                genericResponseObject.IsSuccess = true;
                genericResponseObject.Message = ConstaintStingValue.Tag_ConnectionSuccess;
            return Json(genericResponseObject);
        }

        private IActionResult RedirectToLocal()
        {

                 return RedirectToAction(nameof(DashBoardController.Index), "DashBoard");
        }

The RedirectToAction is not working. RedirectToAction不起作用。 I get the response in json. 我在json中得到响应。 The contoller should navigate to the action but I am getting the json result at the client side. contoller应该导航到该操作,但是我在客户端获得了json结果。

Here is my angular 2 routing 这是我的角度2路由

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { RouterModule, Routes } from '@angular/router';
import { HttpModule, JsonpModule } from '@angular/http';
import { ModuleWithProviders } from '@angular/core';
import { AppComponent} from "./app.component";

import { LoginComponent } from "./Components/login.Component";
import { HomeComponent } from "./Components/home.component";

const appRoutes: Routes = [
    { path: '', redirectTo: 'Home/Index', pathMatch: 'full' },
    { path: 'Account/Login', component: LoginComponent },
    { path: 'Home/Index', component: HomeComponent }
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);


@NgModule({
    imports: [BrowserModule, FormsModule, HttpModule, routing],
    declarations: [AppComponent,LoginComponent, HomeComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }

Please can anyone tell me how to resolve this issue. 请任何人告诉我如何解决此问题。

I suggest the action return Json instead of RedirectToLocal(), for example: 我建议操作返回Json而不是RedirectToLocal(),例如:

return Json(new 
{ 
    isRedirectToWhere = true 
});

then do redirect in ng2 when you get the response 然后在收到响应时在ng2中重定向

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

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