【问题标题】:asp.net core mvc navigation not working with angular 2asp.net 核心 mvc 导航不适用于 Angular 2
【发布时间】:2017-05-20 17:52:45
【问题描述】:

下面是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" });
            });

我正在使用 Angular 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 刷新页面。控制器动作方法被命中,内容被加载。

什么是我无法弄清楚的问题。

【问题讨论】:

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


    【解决方案1】:

    尝试使用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" });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      相关资源
      最近更新 更多