堆栈溢出响应 API
Stack overflow response API
我正在向我的 WebApi Asp.Net Core 3.0 发送一个 POST 请求并收到错误堆栈溢出。
这是为什么?
我的终端回复:
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/2 POST https://localhost:5001/clients application/json 5
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint 'Cardapio.Controllers.ClientsController.Post (Cardapio)'
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
Route matched with {action = "Post", controller = "Clients"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Post(Cardapio.Models.Client) on controller Cardapio.Controllers.ClientsController (Cardapio).
Stack overflow.
我的控制器:
using System.Threading.Tasks;
using Cardapio.Models;
using Microsoft.AspNetCore.Mvc;
namespace Cardapio.Controllers
{
[ApiController]
[Route("clients")]
public class ClientsController : Controller
{
[HttpPost]
[Route("")]
public IActionResult Post([FromBody]Client model)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
return Ok("Success");
}
}
}
我的客户模型:
using System;
using System.ComponentModel.DataAnnotations;
namespace Cardapio.Models
{
public class Client
{
[Key]
public int Id { get; set; }
public string Ip { get; set; }
public string Identification { get; set; }
public string Secrets { get; set; }
public DateTime CreatedAt { get { return this.CreatedAt; } set { this.CreatedAt = DateTime.Now; } }
}
}
这是一个简单的控制器,在另一个具有数据库访问权限的 entity framework 中没有问题。
您的模型有问题,CreatedAt
正在引用自身,这将导致尝试检索或设置值的无限循环。
实现 getter 和 setter 背后的逻辑时需要支持字段。
这是一个使用支持字段的示例。
using System;
using System.ComponentModel.DataAnnotations;
namespace Cardapio.Models
{
public class Client
{
private DateTime createdAt;
[Key]
public int Id { get; set; }
public string Ip { get; set; }
public string Identification { get; set; }
public string Secrets { get; set; }
public DateTime CreatedAt { get { return this.createdAt; } set { this.createdAt = DateTime.Now; } }
}
}
不过,您可以通过为 CreatedAt
分配默认值来最大限度地减少这种情况,因为在 get
或 set
操作中没有发生任何特别的自定义事件。如果 CreatedAt
不希望 return 来自 API 那么你也可以删除 set
.
using System;
using System.ComponentModel.DataAnnotations;
namespace Cardapio.Models
{
public class Client
{
[Key]
public int Id { get; set; }
public string Ip { get; set; }
public string Identification { get; set; }
public string Secrets { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.Now;
}
}
我正在向我的 WebApi Asp.Net Core 3.0 发送一个 POST 请求并收到错误堆栈溢出。 这是为什么?
我的终端回复:
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
Request starting HTTP/2 POST https://localhost:5001/clients application/json 5
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
Executing endpoint 'Cardapio.Controllers.ClientsController.Post (Cardapio)'
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
Route matched with {action = "Post", controller = "Clients"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Post(Cardapio.Models.Client) on controller Cardapio.Controllers.ClientsController (Cardapio).
Stack overflow.
我的控制器:
using System.Threading.Tasks;
using Cardapio.Models;
using Microsoft.AspNetCore.Mvc;
namespace Cardapio.Controllers
{
[ApiController]
[Route("clients")]
public class ClientsController : Controller
{
[HttpPost]
[Route("")]
public IActionResult Post([FromBody]Client model)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
return Ok("Success");
}
}
}
我的客户模型:
using System;
using System.ComponentModel.DataAnnotations;
namespace Cardapio.Models
{
public class Client
{
[Key]
public int Id { get; set; }
public string Ip { get; set; }
public string Identification { get; set; }
public string Secrets { get; set; }
public DateTime CreatedAt { get { return this.CreatedAt; } set { this.CreatedAt = DateTime.Now; } }
}
}
这是一个简单的控制器,在另一个具有数据库访问权限的 entity framework 中没有问题。
您的模型有问题,CreatedAt
正在引用自身,这将导致尝试检索或设置值的无限循环。
实现 getter 和 setter 背后的逻辑时需要支持字段。
这是一个使用支持字段的示例。
using System;
using System.ComponentModel.DataAnnotations;
namespace Cardapio.Models
{
public class Client
{
private DateTime createdAt;
[Key]
public int Id { get; set; }
public string Ip { get; set; }
public string Identification { get; set; }
public string Secrets { get; set; }
public DateTime CreatedAt { get { return this.createdAt; } set { this.createdAt = DateTime.Now; } }
}
}
不过,您可以通过为 CreatedAt
分配默认值来最大限度地减少这种情况,因为在 get
或 set
操作中没有发生任何特别的自定义事件。如果 CreatedAt
不希望 return 来自 API 那么你也可以删除 set
.
using System;
using System.ComponentModel.DataAnnotations;
namespace Cardapio.Models
{
public class Client
{
[Key]
public int Id { get; set; }
public string Ip { get; set; }
public string Identification { get; set; }
public string Secrets { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.Now;
}
}