【问题标题】:Cannot post value to database Using API post method and postman无法使用 API 发布方法和邮递员将值发布到数据库
【发布时间】:2019-05-06 19:56:33
【问题描述】:

我有一个将数据插入数据库的 API 方法,但是当我尝试使用邮递员对其进行测试时,它返回错误请求,这是我的代码

private Utilities uti = new Utilities();
private readonly ApplicationDBContext db;
public AppraisalController(ApplicationDBContext context)
{
    db = context;
}
//INSERT API FOR AppraisalIdentity table
[AllowAnonymous]
[Route("api/appraiseinsert")]
[HttpPost]

public IActionResult Create([FromBody] AppraisalIdentity cre)
{
    if (cre == null)
    {
        return BadRequest();
    }
    using (var transaction = db.Database.BeginTransaction())
    {
        try
        {
            #region Appraisal Insert
            var apprais = new AppraisalIdentity
            {
                AppraisalName = cre.AppraisalName,
                IsCurrent = cre.IsCurrent,
                CompanyID = cre.CompanyID
            };
            db.AppraisalIdentity.Add(apprais);
            db.SaveChanges();
            #endregion
        }
        catch (Exception ex)
        {
            transaction.Rollback();
            return Json(new
            {
                statusCode = ex.Message
            });
        }

    }

    return Json(new
    {
        statusCode = "Success"
    });
}

每当我尝试在 postman man 上测试它时 首先....如果我尝试使用 json,它将返回“错误字符串”。我不知道错误是来自api还是来自我的json插入值的方法......这里使用的是json格式

{
“AppraisalName” : “Appraisal Name”,
“IsCurrent”: 1,
“CompanyID”: 2

}

【问题讨论】:

    标签: c# json linq api


    【解决方案1】:

    '''的符号是什么?我认为正确的 Json 字符串是:

    {
       "AppraisalName" : "Harish",
       "IsCurrent": 1,
       "CompanyID": 2
    }
    

    【讨论】:

    • 是的,看来 OP 正在使用其他一些无效的双引号字符。
    • 非常感谢您的回答,我不会再收到错误的请求了......现在成功......谢谢
    • 问题是引号......但现在解决了
    猜你喜欢
    • 2019-10-23
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 2023-02-16
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多