asp.net核心mvc导航无法使用angular 2

时间:2017-01-06 01:53:45

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

以下是mvc路由模板

 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路由。这是我的 service.login.ts

import { Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { LoginViewModel } from "../ViewModel/Login.ViewModel";

@Injectable()
export class LoginService {
    private heroesUrl = "Account/Authentication";//ApplicationRootUrl("Login", "Home");  // URL to web API
    private ResponseData: any;

    constructor(private loginModel: LoginViewModel, private http: Http, private router: Router) { }

    public loginHttpCall() {
        let headers = new Headers({ 'Content-Type': 'application/json' });
        this.http.post(this.heroesUrl, this.loginModel, { headers: headers })
            .subscribe( data => {
                this.ResponseData = data;
                this.loginModel.success = this.ResponseData.json().isSuccess;
                this.loginModel.message = this.ResponseData.json().message;
                if (this.loginModel.success) {
                    this.router.navigate(['DashBoard/Index']);
                }
            });
    }

}

我可以获得导航 http://localhost:28739/DashBoard/Index 。但是没有加载索引页面的内容。控制器操作方法索引未命中。但是如果我使用相同的URL刷新页面。命中控制器操作方法并加载内容。

我无法解决的问题是什么。

1 个答案:

答案 0 :(得分:0)

尝试使用Microsoft.AspNet.SpaServices。将其安装为nuget包,并通过调用spa-fallback方法替换MapSpaFallbackRoute(找到示例here):

// when the user types in a link handled by client side routing to the address bar or refreshes
// the page, that triggers the server routing. The server should pass that onto the
// client, so Angular can handle the route
routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" });