Angular 将Guid从角度前端传递到Asp.Net核心
我有一个CRUD应用程序,当我将Guid从Angular传递到ASP.NET core时,但我在发布时收到一个“将值转换为类型“System.Guid”的错误。我在这里找不到任何有用的东西。有没有关于如何实现它的提示 这是我在后端的模型:Angular 将Guid从角度前端传递到Asp.Net核心,angular,asp.net-core,Angular,Asp.net Core,我有一个CRUD应用程序,当我将Guid从Angular传递到ASP.NET core时,但我在发布时收到一个“将值转换为类型“System.Guid”的错误。我在这里找不到任何有用的东西。有没有关于如何实现它的提示 这是我在后端的模型: public class ArchiveFile { [Key] public Guid Id { get; set; } [Required] [MaxLength(1
public class ArchiveFile
{
[Key]
public Guid Id { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[Required]
[MaxLength(100)]
public string Code { get; set; }
[ForeignKey("ArchiveBoxId")]
public ArchiveBox ArchiveBox { get; set; }
public Guid ArchiveBoxId { get; set; }
}
public async Task<ActionResult> AddFile(ArchiveFile file)
{
_archiveFileRepository.AddFile(file);
await _archiveFileRepository.SaveChangesAsync();
return CreatedAtRoute("GetFile", new { id = file.Id }, file);
}
这是前端:
export class File {
id: string;
name: string;
code: string;
value: number;
boxId: string;
}
postFile() {
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
}
return this.http.post(this.rootURL + '/files', this.fileFormData, httpOptions)
这是前端的post方法:
export class File {
id: string;
name: string;
code: string;
value: number;
boxId: string;
}
postFile() {
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
}
return this.http.post(this.rootURL + '/files', this.fileFormData, httpOptions)
在后端:
public class ArchiveFile
{
[Key]
public Guid Id { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[Required]
[MaxLength(100)]
public string Code { get; set; }
[ForeignKey("ArchiveBoxId")]
public ArchiveBox ArchiveBox { get; set; }
public Guid ArchiveBoxId { get; set; }
}
public async Task<ActionResult> AddFile(ArchiveFile file)
{
_archiveFileRepository.AddFile(file);
await _archiveFileRepository.SaveChangesAsync();
return CreatedAtRoute("GetFile", new { id = file.Id }, file);
}
公共异步任务AddFile(ArchiveFile文件)
{
_archiveFileRepository.AddFile(文件);
wait_archiveFileRepository.saveChangesSync();
返回CreatedAtRoute(“GetFile”,新的{id=file.id},file);
}
看起来GUID值没有传递到API。在这个.fileFOrmData中应该传递到哪里?如果是这样,我会添加一个console.log或调试器,并确保这个.fileFOrmData的Id设置正确Hi@PabloAguirredeSouza,您检查了请求和实际发布的数据了吗?