[英]asp.net core mvc navigation not working with angular 2
這是下面的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刷新頁面。 點擊控制器動作方法並加載內容。
我無法解決的問題是什么?
嘗試使用Microsoft.AspNet.SpaServices
。 將其安裝為nuget軟件包,並通過調用MapSpaFallbackRoute
方法( 在此處找到示例)替換spa-fallback
:
// 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" });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.