C# POST API问题已成功过帐,但仍返回500个错误代码
我有一个C# POST API问题已成功过帐,但仍返回500个错误代码,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我有一个POSTAPI,看起来是这样的: [Route("api/Review/post")] [HttpPost] [AcceptVerbs("POST")] // POST: api/Review [ResponseType(typeof(review))] public IHttpActionResult Postreview(review review) { if (!ModelState.IsValid)
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);
}
当我点击按钮时,我希望它返回“Success”,并将我重定向到我的ReviewSubmitted.aspx
页面。然而,即使POST
成功运行,当它返回到按钮_click
方法时,它告诉我有一个500错误
我已经在数据库中验证了这篇文章是成功的。这只是一个个人项目,永远不会去任何地方,所以我可以告诉它在失败时重定向,但我不想这样做,因为显而易见的原因
我错过了什么?为什么
POST
ing仍然说有错误 我解决了这个问题。我首先发现了一个问题,它引导我下载高级REST客户端(ARC)
然后,我能够确定API返回JSON内容中的实际错误:
"Message": "An error has occurred.",
"ExceptionMessage": "A route named 'DefaultApi' could not be found in the route collection. Parameter name: name"
,这个问题为我解决了:
您调试并逐步完成了操作吗?我调试并逐步完成了,这就是我如何知道这是错误的原因。即使代码仍在发布。除了
响应之外,它都正常运行。IsSuccessStatusCode
返回为500。我指的是服务器端操作是的,它成功地通过了服务器端操作,没有任何问题,并且发布了结果。如果确认了,那么唯一的其他原因可能是来自管道中的中间件。检查是否添加了中间件(如果有)。