【问题标题】:Issue with POST API successfully POSTing but still returning 500 error codePOST API 成功发布但仍返回 500 错误代码的问题
【发布时间】:2018-09-07 04:00:43
【问题描述】:

我有一个看起来像这样的POST API:

    [Route("api/Review/post")]
    [HttpPost]
    [AcceptVerbs("POST")]
    // POST: api/Review
    [ResponseType(typeof(review))]
    public IHttpActionResult Postreview(review review)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.review.Add(review);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateException)
        {
            if (reviewExists(review.rating_id))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

        return CreatedAtRoute("DefaultApi", new { id = review.rating_id }, review);
    }

在表单上,​​我有一个按钮,该按钮从表单上的字段中绘制以通过POST API 运行。它看起来像这样:

    protected void Button1_Click(object sender, EventArgs e)
    {


        void Add_Review(int ap_id, int usr, string val, DateTime revdate, double scr, string com)
        {
            if (ap_id == 0)
            {
                throw new ArgumentException(message: "Please select a site");
            }
            if (string.IsNullOrEmpty(com))
            {
                throw new ArgumentException(message: "Please add a comment.");
            }
            using (var client = new HttpClient())
            {
                review r = new review
                {
                    site_id = ap_id,
                    usr_id = usr,
                    valid = val,
                    review_date = revdate,
                    score = scr,
                    comments = com
                };
                client.BaseAddress = new Uri("http://localhost:#####/");
                var response = client.PostAsJsonAsync("api/Review/post", r).Result;
                if (response.IsSuccessStatusCode)
                {
                    Console.Write("Success");

                    Response.Redirect("ReviewSubmitted.aspx");

                }
                else
                    Console.Write("Error");
            }
        }
        int usrid;
        usrid = Convert.ToInt32(ConfigurationManager.AppSettings["TestUserID"]);

        Add_Review(Convert.ToInt16(AirportList.SelectedValue),
            usrid,
            "Y",
            Convert.ToDateTime(DateTime.Now.ToString("M/d/yyyy")),
            Convert.ToDouble(textScore.Text),
            textComments.Text);
    }

当我单击该按钮时,我希望它返回“成功”并将我重定向到我的ReviewSubmitted.aspx 页面。然而,即使POST 运行成功,当它移回button_click 方法时,它告诉我我有一个500 错误。

我已在数据库中验证 POST 成功。这只是一个永远不会去任何地方的个人项目,所以我也可以告诉它在失败时重定向,但出于明显的原因,我不想这样做。

我在这里缺少什么?为什么是POSTing,却还是说有错误?

【问题讨论】:

  • 您是否已调试并逐步完成操作?
  • 我有,这就是我知道这是错误的方式。即使代码仍在发布。除了 response.IsSuccessStatusCode 返回为 500 之外,它都正常运行。
  • 我指的是服务器端动作
  • 是的,它成功通过服务器端操作,没有任何问题,POSTs 结果。
  • 好的,如果确认,那么唯一的其他原因可能来自管道中的中间件。检查添加了哪些中间件。

标签: c# asp.net asp.net-web-api


【解决方案1】:

我发现了这个问题。我首先发现了一个引导我下载 Advanced REST Client (ARC) 的问题。

然后我能够确定 API 返回 JSON 内容中的实际错误:

"Message": "An error has occurred.",
"ExceptionMessage": "A route named 'DefaultApi' could not be found in the route collection. Parameter name: name" 

,这是为我解决的问题:

Attribute Routing and CreatedAtRoute

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多