Web api插入失败但代码似乎很好。可能有什么不对? [英] Web api insert failed but code seems fine. What could be wrong?
本文介绍了Web api插入失败但代码似乎很好。可能有什么不对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi
对不起,我在讨论中发布了这个,我认为这是一个错误的地方,我转发到这里。
我不知道哪里出了问题,但我似乎无法使用web api插入数据。任何帮助非常感谢和非常感谢
数据
hi
Sorry i posted this in the discussion which i think is a wrong place and i repost here.
I do not know where is going wrong but i seems not able to insert data using web api. Any help greatly appreciated and thanks a lot
The data
{"ID":1,"EmailAddress":"demo@demo.com"}
和代码:
and the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Dapper;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace cirqer.Controllers.api
{
public class EmailListList
{
public int ID { get; set; }
public string EmailAddress { get; set; }
}
public class emaillistController : ApiController
{
static string connectionstring = ConfigurationManager.ConnectionStrings["CRConnectionString"].ConnectionString;
[HttpGet]
public Int16 EmailList_SelectCount()
{
try
{
using (var conn = new SqlConnection(connectionstring))
{
Int16 x = conn.ExecuteScalar("EmailList_SelectCount", commandType: CommandType.StoredProcedure);
return x;
}
}
catch
{
return 0;
}
}
[HttpPost]
public void EmailList_Insert(EmailListList email)
{
try
{
// dynamic data = email;
using (var conn = new SqlConnection(connectionstring))
{
conn.Open();
conn.Execute("EmailList_Insert", email, commandType: CommandType.StoredProcedure);
conn.Close();
}
}
catch
{
}
}
我尝试过:
使用telerik fiddler进行模拟插入但失败。
What I have tried:
Using telerik fiddler to simulate insert but failed.
推荐答案
删除try / catch块。你的代码可能会抛出异常,但是因为你拥有了try / catch块中的所有内容,而catch部分没有任何代码可以在任何地方记录异常,只是异常被吞下。
Remove the try/catch blocks. Your code is probably throwing exceptions but since you have everything in try/catch blocks and the catch sections don't have any code to log exceptions anywhere the exceptions are just being quitely swallowed.
这篇关于Web api插入失败但代码似乎很好。可能有什么不对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文