對象我有一個角狀的部件:調用.NET核心控制器get方法與angular2
import { Component } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'search',
template: require('./search.component.html'),
styles: [require('./search.component.css')]
})
export class SearchComponent {
public tumorType = "Tymor Type";
public gene = "Gene";
public searchList: any[];
public searchCriteria:
{
TumorType: string;
Gene: string;
Country: string;
} =
{
TumorType: "Acute Lymphoblastic Leukemia",
Gene: "ABL",
Country: "Australia"
};
constructor(private http: Http) {
}
public search() {
let searchCrit = this.searchCriteria;
this.http.get('/api/RocheClinicalTrial/' + JSON.stringify({ searchCrit })).subscribe(result => {
//this.searchList = result.json();
console.log(result);
});
}
}
和MVC控制器
namespace RocheClinicalTrial.Controllers
{
[AllowAnonymous]
[Route("api/[controller]")]
public class RocheClinicalTrialController : Controller
{
// GET: api/values
[HttpGet]
public JsonResult Get()
{
var repo = new Repository.Repository();
var data = repo.GetData();
return Json(data);
}
[Route("GetData")]
//GET api/values/5
[HttpGet("api/values")]
public string Get(string searchCriteria)
{
System.Console.WriteLine(searchCriteria);
return "value";
}
}
}
這應該叫
獲取(string searchCriteria)
方法。但電話沒有發生。但是,當我打電話而不值
this.http.get( '/ API/RocheClinicalTrial /'/ + JSON.stringify({ searchCrit}) /).subscribe(result => {// this.searchList = result.json(); console.log(result); });
它調用
公共JsonResult獲得()
方法。你能幫忙嗎?
下面是從網絡選項卡中的通話截圖
你檢查網絡選項卡?看看什麼和請求去了哪裏..? –
是的,我做過。它稱爲「http:// localhost:52086/api/RocheClinicalTrial /%7B%22searchCrit%22:%7B%22TumorType%22:%22Acute%20Lymphoblastic%20Leukemia%22,%22Gene%22:%22ABL%22,%22Country %22:%22Australia%22%7D%7D「 即使我直接命中url localhost:52086/api/RocheClinicalTrial/something。通話不會去那裏。 – Anurag
嘗試控制整個請求url,看看有什麼區別 –