C# 如何在数据库中记录Http通信错误
我的.Net MVC应用程序正在尝试一个HttpC# 如何在数据库中记录Http通信错误,c#,angular,asp.net-mvc,typescript,C#,Angular,Asp.net Mvc,Typescript,我的.Net MVC应用程序正在尝试一个Httpput调用来更新现有记录。我注意到控制器put逻辑没有像其他http通信那样被触发 我想包括上找到的HandleError逻辑来写出错误。当我在数据服务层中包含错误处理程序时,我得到的类型为“Observable”的参数不能分配给类型为“(err:any,catch:Observable)=>ObservableInput”的参数 根据我在上看到的内容,我得到了正确的JSON对象和API url。如果我将JSON对象和URL复制到Postman中,
put
调用来更新现有记录。我注意到控制器put
逻辑没有像其他http通信那样被触发
我想包括上找到的HandleError逻辑来写出错误。当我在数据服务层中包含错误处理程序时,我得到的类型为“Observable”的参数不能分配给类型为“(err:any,catch:Observable)=>ObservableInput”的参数
根据我在上看到的内容,我得到了正确的JSON对象和API url。如果我将JSON对象和URL复制到Postman中,就可以访问控制器
如能提供有关错误处理和日志记录的任何见解,将不胜感激
组件逻辑:
updateRecord(record_id: number, newRecord: any): void
{
this.recordService.put<Record>(record_id, newRecord);
}
控制器逻辑:
[HttpPut("{id}")]
public async Task<ActionResult<Domain.Record>> Put(int id, [FromBody] Domain.Record record)
{
//Confirm the request record and ID record being update match
if (id != record.record_id)
return BadRequest();
//Modify the state
_context.Entry(record).State = EntityState.Modified;
//Update the records in DB.records, throw appropriate error if there is one.
try
{
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if (!RecordExists(record.record_id))
return NotFound();
else
throw;
}
//return 200 OK
return NoContent();
}
[HttpPut(“{id}”)]
公共异步任务Put(int id,[FromBody]域.记录)
{
//确认正在更新的请求记录和ID记录匹配
if(id!=record.record\u id)
返回请求();
//修改状态
_context.Entry(record).State=EntityState.Modified;
//更新DB.records中的记录,如果有错误,则抛出相应的错误。
尝试
{
wait_context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
如果(!RecordExists(record.record_id))
返回NotFound();
其他的
投掷;
}
//返回200 OK
返回NoContent();
}
根据对主要问题的评论,您似乎遗漏了某个订阅位置
在RXJS中,未订阅的可观察对象将永远不会执行。那么这个,
updateRecord(record_id: number, newRecord: any): void
{
this.recordService.put<Record>(record_id, newRecord);
}
updateRecord(记录id:number,新记录:any):无效
{
this.recordService.put(record\u id,newRecord);
}
应转变为:
updateRecord(record_id: number, newRecord: any): void
{
this.recordService.put<Record>(record_id, newRecord).subscribe((result) => {
// process results here
};
}
updateRecord(记录id:number,新记录:any):无效
{
this.recordService.put(record\u id,newRecord).subscribe((result)=>{
//在此处理结果
};
}
如果没有订阅,我不相信您会从
recordService.put()
from中的HTTP调用中得到任何结果。问题可能在于您的handleError()
函数。你也可以添加它吗?我不清楚never
或Observable`是在哪里定义的,也不清楚错误指的是什么;也不清楚你是否订阅过数据服务put
方法的结果。@JeffryHouser你让我找到了正确的方向。我没有订阅对组件的调用。你知道吗在回答这个问题时,我会快速地介绍一下为什么订阅很重要?我会把它标记为正确答案。
updateRecord(record_id: number, newRecord: any): void
{
this.recordService.put<Record>(record_id, newRecord);
}
updateRecord(record_id: number, newRecord: any): void
{
this.recordService.put<Record>(record_id, newRecord).subscribe((result) => {
// process results here
};
}