RedirectToAction在asp.net核心中不使用angular2

时间:2017-01-04 01:53:25

标签: c# asp.net asp.net-mvc angular asp.net-core

我正在使用带有asp.net core的Angular 2.这是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" });
            });

这是行动方法。动作方法是从角度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");
        }

RedirectToAction不起作用。我得到了json的回复。控制器应该导航到操作,但我在客户端获得json结果。

这是我的角度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 { }

请有人告诉我如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

我建议操作返回Json而不是RedirectToLocal(),例如:

return Json(new 
{ 
    isRedirectToWhere = true 
});

然后在获得响应时在ng2中重定向