我知道我确实想念一些东西。我的整个项目在某种程度上是各种“如何...”的复制和粘贴,我的 C# 知识充其量只是基础知识,我需要让它工作,因为我们的标准 Web 服务软件仅在发送时才是 RESTful。
我的主要问题是,我偶然发现的所有解决方案实际上都是代码片段,这对我来说不起作用 - 我的 C# 知识很基础,所以我不明白它是如何工作的,更不用说对其进行故障排除了。我很确定我什至没有捕获随传入请求一起发布的 JSON。但我可能错了。
要求:在 WS2012R2 上的 IIS 上运行的东西,可以通过 HTTPPost 接受 JSON 文件,将内容转储到 SQL Server 表中,并将刚刚创建的行的 Id 返回给 JSON 的发送者。我必须在它的基础上进行构建才能获得成熟的 Web 服务,该服务可以发送和接收包含不同数据的多个 JSON 文件,所有这些文件都必须最终存储在 SQL Server 中。
我拥有的:
Class:
namespace NA.Models
{
public class Note
{
public Note() { }
//public Guid id { get; set; }
public static string Client { get; set; }
public static int Case { get; set; }
public static string Text { get; set; }
public static int NoteId { get; set; }
public static string R1 { get; set; }
public static string R2 { get; set; }
public static string S1 { get; set; }
public static DateTime Date { get; set; }
public static bool Type { get; set; }
}
}
界面:
namespace NA.Models
{
interface INoteRepository
{
IEnumerable<Note> GetAll();
void Add(Note item);
}
}
存储库:
namespace NA.Models
{
class NoteDataRepository : INoteRepository
{
public void Add(Note item)
{
if (item == null)
{
throw new ArgumentNullException("item");
}
else
{
String strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BL_IntegrationInsertNote";
cmd.Parameters.Add("@Client", SqlDbType.VarChar).Value = item.Client.Trim();
cmd.Parameters.Add("@Case", SqlDbType.VarChar).Value = item.case;
cmd.Parameters.Add("@Text", SqlDbType.VarChar).Value = item.Text.Trim();
cmd.Parameters.Add("@When", SqlDbType.DateTime).Value = item.Date;
cmd.Parameters.Add("@Ext", SqlDbType.Bit).Value = item.Type;
cmd.Parameters.Add("@return", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
string id = cmd.Parameters["@return"].Value.ToString();
string lblMessage = null;
lblMessage = "Record inserted successfully. ID = " + id;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
return item;
}
IEnumerable<Note> INoteRepository.GetAll()
{
throw new NotImplementedException("getitems");
}
}
}
控制器:
namespace NA.Controllers
{
public class NC : ApiController
{
[Route("AddNote")]
[HttpPost]
public HttpResponseMessage PostNote(List<Note> item)
{
//NoteJson deserializednote = JsonConvert.DeserializeObject<NoteJson>(item);
//Note notesdata = new Note(item);
//foreach (deserializednote
NotesAccept.Models.INoteRepository Repository = new NotesAccept.Models.NoteDataRepository();
item = Repository.Add(item);
var response = Request.CreateResponse < NotesAccept.Models.Note>(HttpStatusCode.Created, item);
return response;
}
}
}
当尝试将测试 json 发送到服务时,我收到错误返回:
500:服务器内部错误,参数项的值不能为空
这是 posttestserver.com 发送的请求的转储:
Headers (Some may be inserted by server)
REQUEST_URI = /post.php
QUERY_STRING =
REQUEST_METHOD = POST
GATEWAY_INTERFACE = CGI/1.1
REMOTE_PORT = 56926
REMOTE_ADDR = ip
HTTP_CONNECTION = close
HTTP_CACHE_CONTROL = max-age=259200
HTTP_X_FORWARDED_FOR = 172.16.3.87
HTTP_VIA = 1.1 koenig.local (squid/3.3.13)
HTTP_EXPECT = 100-continue
CONTENT_LENGTH = 153
HTTP_HOST = posttestserver.com
HTTP_ACCEPT = application/json
CONTENT_TYPE = application/json
UNIQUE_ID = Vri0cUBaMGUAABvGeesAAAAL
REQUEST_TIME_FLOAT = 1454945393.4611
REQUEST_TIME = 1454945393
No Post Params.
== Begin post body ==
[{
"Client": "Client1",
"Case": 1,
"Text": "Text",
"NoteId": 2,
"R1": "R1",
"R2": "R2",
"S1": "S1",
"Date": "2015-10-26T09:06:46",
"Type":"1"
}]
== End post body ==
Upload contains PUT data:
[{
"Client": "Client1",
"Case": 1,
"Text": "Text",
"NoteId": 2,
"R1": "R1",
"R2": "R2",
"S1": "S1",
"Date": "2015-10-26T09:06:46",
"Type":"1"
}]
上面的转储来自 POST 请求,与我发送到 Web 服务的请求相同,但 URL 除外。所以可以视为实际请求。
这是 IIS 日志:
字段:日期时间 s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus
sc-win32-status 所用时间 2016-02-08 15:49:52 ::1 POST /blz/AddNote -
80 - ::1 - - 500 0 0 2937