1
我有如下一個簡單的MVC獲取方法從會話試圖調用MVC從角2取得動作與打字稿
[HttpGet]
public string GetCustomerId()
{
return Session["CUSTOMERID"].ToString();
}
獲得客戶ID如果我直接在瀏覽器 http://localhost/myApp/Home/GetCustomerId我打這個網址可以在方法中設置一個斷點,並且它會被打中,並且我得到返回的值。
但是,我需要從我的客戶端代碼中調用Angular 2編寫的方法。我的Typescript方法如下 - 即使我將上述完全相同的URL記錄到控制檯,我也無法使它達到MVC斷點。
public getCustomerIdFromSession() {
console.log('get customer from session called');
let srv = this.environmentService.getCurrentEnvironment();
let httpOrHttps = '';
if (srv === AppSettings.ENV_LOCALHOST) {
httpOrHttps = AppSettings.URL_HTTP;
}
else {
httpOrHttps = AppSettings.URL_HTTPS;
}
let baseUrl = httpOrHttps + srv + AppSettings.URL_GET_CUST_FROM_SESSION;
console.log(baseUrl); //this logs - http://localhost/myApp/Home/GetCustomerId
return this.http.get(baseUrl)
.catch(this.handleError);
}
public handleError(error: Response) {
console.log("error");
return Observable.throw(error.json() || 'Server Error');
}
**更新到包括整個打字稿服務
import { Injectable, Output, EventEmitter } from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { EnvironmentService } from '../common/environment.service';
import { AppSettings } from '../common/app-settings';
@Injectable()
export class SessionService {
@Output() public gSession: EventEmitter<any> = new EventEmitter();
private sessionTime: number = 1500000; // 25 minute
constructor(private http: Http, private environmentService: EnvironmentService) {
}
public setValue(isLoading: boolean): void {
this.gSession.emit(isLoading);
}
public getValue(): any {
return this.gSession;
}
public startSession(): void {
this.getCustomerIdFromSession();
let timeoutId = setTimeout(() => {
this.setValue(true);
}, this.sessionTime);
}
public getCustomerIdFromSession() {
console.log('get customer from session called');
let srv = this.environmentService.getCurrentEnvironment();
let httpOrHttps = '';
if (srv === AppSettings.ENV_LOCALHOST) {
httpOrHttps = AppSettings.URL_HTTP;
}
else {
httpOrHttps = AppSettings.URL_HTTPS;
}
let baseUrl = httpOrHttps + srv + AppSettings.URL_GET_CUST_FROM_SESSION;
console.log(baseUrl); //this logs - http://localhost/myApp/Home/GetCustomerId
return this.http.get(baseUrl)
.catch(this.handleError);
}
public handleError(error: Response) {
console.log("error");
return Observable.throw(error.json() || 'Server Error');
}
public extractData(res: Response) {
console.log("In extract method");
let body = res.json();
console.log(body);
if (body) {
return body.data || body;
} else {
return {};
}
}
}
即使添加的提取方法仍然沒有得到方法中的MVC斷點命中 - 將一個console.log(「提取方法」)行添加到提取方法的開始,但它永遠不會獲得該方法 –
打印到Chrome控制檯 –
@Ctrl_Alt_Defeat您可以發佈整個服務嗎?你的構造函數中有'Http'類嗎?更新了 – SrAxi