简体   繁体   English

POST 方法返回 500 错误(ASP.net Core)

[英]POST method is returning 500 error (ASP.net Core)

I have been getting 500 error when I tested my POST method in postman and I really have no idea why no matter how my times I modified my code.当我在 postman 中测试我的 POST 方法时,我遇到了 500 错误,我真的不知道为什么无论我如何修改我的代码。

Here is the model I added after connecting to the external db in MSSQL server:这是我在连接到 MSSQL 服务器中的外部数据库后添加的 model:

  public partial class Shipment
    {
        public Shipment()
        {
            Packages = new HashSet<Package>();
        }

        /// <summary>
        /// Shipment Unique Identifier
        /// </summary>
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid SSuid { get; set; }
        public string? SType { get; set; }
        public string? SFrom { get; set; }
        public string? SCompanyName { get; set; }
        public string? SFname { get; set; }
        public string? SMname { get; set; }
        public string? SLname { get; set; }
        public string? SAddress1 { get; set; }
        public string? SAddress2 { get; set; }
        public string? SCity { get; set; }
        public string? SState { get; set; }
        public string? SPostalCode { get; set; }
        public string? SCountry { get; set; }
        public string? SPo { get; set; }
        public DateTime SDateReceived { get; set; }
        public DateTime SDateEntered { get; set; }
        public string? SUserName { get; set; }
        public virtual ICollection<Package> Packages { get; set; }
    }

Here is my repository:这是我的存储库:

        public async Task AddShipment(Shipment shipment)

        {             
            await _operationsContext.Shipments.AddAsync(shipment);
            await _operationsContext.SaveChangesAsync();
   }

        public async Task<Shipment> GetById(Guid id)
        {
            return await _operationsContext.Shipments.FirstOrDefaultAsync(s => s.SSuid == id);
        }

Here are my controllers:这是我的控制器:

[Produces("application/json")]
[Route("api/shipment")]
[ApiController]
public class ShipmentController : Controller
{
    private readonly IShipmentRepository _shipmentRepository;

    [HttpGet("{ssuid}", Name = "GetShipmentById")]
        public async Task<ActionResult<Shipment>> GetShipmentById(Guid ssuid)
        {
            try
            {
                var result = await _shipmentRepository.GetById(ssuid);

                if(result == null)
                {
                    return BadRequest();
                }
                return result;
            } catch (Exception)
            {
                return StatusCode(StatusCodes.Status500InternalServerError,
                    "Error retrieving data from the database");
            }
        }

        [HttpPost]
        public async Task<ActionResult<Shipment>> AddShipment([FromBody] Shipment shipment)
        {
            try
            {
                await _shipmentRepository.AddShipment(shipment);
                return CreatedAtRoute(nameof(GetShipmentById), new {id = shipment.SSuid}, shipment);
            } catch (Exception)
            {
                return StatusCode(StatusCodes.Status500InternalServerError);

            }
        }

} }

When I tried to add a new shipment当我尝试添加新货件时

{
    "sType": "UPS",
    "sFrom": "Mike",
    "sCompanyName": "",
    "sFname": "David",
    "sMname": "",
    "sLname": "Beckham",
    "sAddress1": "1990 e 12th st",
    "sAddress2": "",
    "sCity": "Irvine",
    "sState": "California",
    "sPostalCode": "75655",
    "sCountry": "United States",
    "sPo": "",
    "sDateReceived": "2022-09-09T00:00:00",
    "sDateEntered": "2022-09-09T10:56:24.617",
    "sUserName": "mbeckham"
    }

in postman, I get在 postman 中,我得到

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.6.1",
    "title": "An error occurred while processing your request.",
    "status": 500,
    "traceId": "00-fa8bb4a7d8b5372766baa5d2c1030b39-29b3770b4eb356de-00"
}

I would appreciate any helps from the experts!我将不胜感激专家的任何帮助!

Hard to say what the exact issue might be, but have u set the newid() for GUID field SSuid in Database.很难说确切的问题可能是什么,但是您是否为数据库中的 GUID 字段 SSuid 设置了newid() Assuming its the PK, decorate the property with [KEY] attribute.假设它是 PK,用[KEY]属性装饰属性。

Please put a try catch block and post the exact error.请放置一个 try catch 块并发布确切的错误。

It looks like you have a constructor dependency.看起来你有一个构造函数依赖。 Maybe you should make sure that you register a concrete implementation of IShipmentRepository when you configure IoC.也许您应该确保在配置 IoC 时注册 IShipmentRepository 的具体实现。 For example as follows:例如如下:

x.For<IShipmentRepository>().Use<MyService>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM