我想将一个新创建的对象传递给我的C#控制器。但是我总是在通过Angular发出的HTTP Post请求中得到一个服务器错误400。
我用同样的方式发出HTTP GET请求,但它工作起来没有任何问题。我的C#控制器:
[Authorize]
[ApiController]
[Produces("application/json")]
[Route("[controller]")]
public class SiteSettingsController : ControllerBase
{
[HttpGet]
public IEnumerable<SiteSettings> Get()
{
return GetAllSiteSettings();
}
[HttpPost]
public Task<IActionResult> Post([FromBody]SiteSettings siteSettings)
{
try
{
return null;
}
catch (Exception e)
{
return null;
}
return null;
}
}和我的角度需求:
save() {
const settings: SiteSettings = {
id: '',
name: 'Test',
domain: 'test.local',
active: false
};
const body = JSON.stringify(settings);
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
return this.http.post<any>(this.baseUrl + 'sitesettings', body, httpOptions).subscribe({
error: error => console.error('There was an error!', error)
});
}发布于 2020-01-31 02:20:33
我太愚蠢了,错过了真正的错误信息。所以要集中注意力,仔细观察...
0: "The JSON value could not be converted to System.Guid. Path: $.id | LineNumber: 0 | BytePositionInLine: 8."https://stackoverflow.com/questions/59992110
复制相似问题