【发布时间】:2017-10-04 00:04:22
【问题描述】:
嘿(第一次在这里发帖,所以我可能会违反一些规则,如果我这样做,请告诉我),
我正在尝试创建一个 Rest API,但遇到了一些问题。 事实上,post 函数并没有在 C# API 上触发,我不知道为什么与 getAll() 函数一起使用的 GET 函数运行良好。
角度服务:
public GetAll = (): Observable<Expertise[]> => {
return this.http.get(this.actionUrl, '').map((response: Response) => <Expertise[]>response.json());
}
public Add = (thingToAdd: Expertise): Observable<Expertise> => {
thingToAdd.Id = 1;
let toAdd = JSON.stringify(thingToAdd);
console.log(toAdd);
console.log(this.actionUrl);
return this.http.post(this.actionUrl, toAdd, { headers: this.headers
}).map((response: Response) => response.json());
}
C# API:
// GET api/values
[HttpGet]
public async Task<IEnumerable<Expertise>> Get()
{
try
{
System.Console.WriteLine("Test get all");
//var result = await cvService.Get("toto@azeo.com");
return new List<Expertise>();
}
catch (System.Exception ex)
{
logger.LogError(ex.Message, ex);
throw;
}
}
// POST api/values
[HttpPost]
public Expertise Post([FromBody]Expertise expertise)
{
try
{
System.Console.WriteLine("Test post");
context.Expertises.Add(expertise);
context.SaveChanges();
logger.LogInformation("New expertise added !");
return expertise;
}
catch (System.Exception ex)
{
logger.LogError(ex.Message, ex);
throw;
}
}
专业知识(EF 模型):
public class Expertise
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public ICollection<ExpCV> CVs { get; set; }
}
如果有人有“链接”服务的想法并且我的 API 告诉我,我已经坚持了很长时间。
提前谢谢你
【问题讨论】:
-
http请求执行了吗?回报是多少?
-
post 好像没有被执行,get 被执行了
-
你如何称呼你的 'getAll' 和 'add' ?您必须订阅它们中的每一个才能进行调用。
-
ngOnInit() { this.expertise = new Expertise(); this.listeExpertise = this.service.GetAll(); } 公共 addExpertise() { this.service.Add(this.expertise); } @AhmedMusallam
-
此代码来自 typescript 中的组件
标签: c# angular typescript angular2-services azure-api-apps