0

I have a POST API that looks like so:

    [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);
    }

On a form, I have a button that is drawing from the fields on the form to run through the POST API. It looks like this:

    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);
    }

When I click the button, I would expect that it returns "Success" and redirects me to my ReviewSubmitted.aspx page. However, even though the POST runs successfully, when it moves back to the button_click method, it tells me that I have a 500 error.

I have verified in the database that the POST was successful. This IS just a personal project that's never going to go anywhere, so I could just tell it to redirect on failure too, but I don't want to do that for obvious reasons.

What am I missing here? Why is it POSTing, but still saying that there was an error?

phroureo
  • 377
  • 3
  • 16
  • Have you debugged and stepped through the action? – Nkosi Sep 05 '18 at 22:04
  • I have, and that's how I know that that's the error. Even though the code is still posting. It all runs normally except for the `response.IsSuccessStatusCode` returning as 500. – phroureo Sep 05 '18 at 22:08
  • I am referring to the server side action – Nkosi Sep 05 '18 at 22:11
  • Yes, it successfully makes it through the server side action without any issues and `POST`s the results. – phroureo Sep 05 '18 at 22:17
  • Ok if that is confirmed then the only other cause could be from a middleware in the pipeline. check to see what middleware if any have been added. – Nkosi Sep 05 '18 at 22:19
  • There is no middleware. There's the button that refers to that click event, and the click event sends it to the API, and the API `POST`s but returns as an error code. – phroureo Sep 05 '18 at 22:20
  • I do not think you are understanding what I am referring to. – Nkosi Sep 05 '18 at 22:21
  • How did you setup the API? If it is returning 500 error then while debugging you should be able to see the exception happening on the API server. in your output window. – Nkosi Sep 05 '18 at 22:22
  • I'm sure I'm misunderstanding. :) It was auto-generated from my model by Visual Studio, which was auto-generated by the database by VS. I did not modify the `POST` action at all (besides adding the stuff in brackets at the top). – phroureo Sep 05 '18 at 22:24
  • Okay, it looks like it has stopped submitting the information to the database now. It's not triggering any of the `CATCH`es in the code for the API, but it's also not submitting to the database, and it's also not returning a success. – phroureo Sep 06 '18 at 18:08

1 Answers1

0

I figured out the issue. I first found a question that directed me to downloading Advanced REST Client (ARC).

I was then able to determine what my actual error was in the API return JSON stuff:

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

, and this is the question that solved it for me:

Attribute Routing and CreatedAtRoute

phroureo
  • 377
  • 3
  • 16