下面是幫一位 使用vb.net的朋友做的一個最簡單 rest接口示例 ,詳細源碼見文章底部
最終效果如下:
默認查詢 返回json
http://localhost:54370/Home/Index
默認創建表
http://localhost:54370/home/createtable
單記錄插表
測試調用代碼
多記錄上傳插表
測試調用代碼
vs2019 新建mvc工程
服務端代碼
Imports FreeSql Imports Newtonsoft.Json Public Class HomeController Inherits System.Web.Mvc.Controller Private Shared ykt As IFreeSql Shared Sub New() ykt = (New FreeSqlBuilder().UseAutoSyncStructure(True)).UseConnectionString(DataType.Sqlite, "Data Source=freedb.db", Nothing).Build() End Sub Public Function createtable() As String Return ykt.[Select](Of tech_info)().Any().ToString() End Function Public Function Index() As ActionResult Dim dict As IDictionary(Of String, Object) = New Dictionary(Of String, Object)() From { {"StateCode", 200} } Dim now As DateTime = DateTime.Now dict.Add("Message", String.Concat("接口訪問成功,服務器時間", now.ToString())) Return MyBase.Json(dict, JsonRequestBehavior.AllowGet) End Function Public Function testUplaod(ByVal xx As List(Of ent_test)) As System.Web.Mvc.ActionResult Dim actionResult As System.Web.Mvc.ActionResult Dim retcode As Integer = -1 Dim t2 As Integer = 0 Dim key As String = DateTime.Now.ToString("yyyyMMddffff") tools.log(String.Concat(key, "接口被調用----------測試上傳方法testuplaod------------------------------")) Dim sendstring As String = JsonConvert.SerializeObject(xx) 'tools.log(String.Concat(key, "傳入數據爲:", sendstring)) Try t2 = ykt.Insert(Of ent_test)(xx).ExecuteAffrows() tools.log(String.Concat(key, "數據插入行數-----", t2.ToString())) Return MyBase.Json(New With {Key .StateCode = 200, Key .Message = String.Concat("測試上傳成功!行數:", t2.ToString(), "數據爲:", sendstring)}, JsonRequestBehavior.AllowGet) Catch exception As System.Exception Dim ex As System.Exception = exception actionResult = MyBase.Json(New With {Key .StateCode = retcode, Key .Message = String.Concat(sendstring, ex.Message)}, JsonRequestBehavior.AllowGet) End Try Return actionResult End Function Public Function upload_tech_info(ByVal xx As tech_info) As System.Web.Mvc.ActionResult Dim actionResult As System.Web.Mvc.ActionResult Dim retcode As Integer = -1 Dim t2 As Integer = 0 Dim key As String = DateTime.Now.ToString("yyyyMMddffff") Dim sendstring As String = JsonConvert.SerializeObject(xx) tools.log(String.Concat(key, "接口被調用----------upload_tech_info-------", sendstring, "-----------------------")) Try t2 = ykt.Insert(Of tech_info)(xx).ExecuteAffrows() Return MyBase.Json(New With {Key .StateCode = 200, Key .Message = String.Concat("上傳工藝信息成功!行數:", t2.ToString(), "數據爲:", sendstring)}, JsonRequestBehavior.AllowGet) Catch exception As System.Exception Dim ex As System.Exception = exception actionResult = MyBase.Json(New With {Key .StateCode = retcode, Key .Message = String.Concat(sendstring, ex.Message)}, JsonRequestBehavior.AllowGet) End Try Return actionResult End Function End Class
測試客戶端代碼
Imports System.IO Imports System.Net Imports Newtonsoft.Json Imports System.Text Module Module1 Sub Main() '印刷rest接口測試() 工單狀態上傳() End Sub '多記錄上傳 Private Sub 印刷rest接口測試() Dim postdata As List(Of ent_test) = New List(Of ent_test)() From { New ent_test() With { .usercode = "001", .username = "張三" }, New ent_test() With { .usercode = "002", .username = "李四" }, New ent_test() With { .usercode = "003", .username = "王五" } } Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://localhost:54370/home/testUplaod"), HttpWebRequest) req.Method = "POST" req.ContentType = "application/json" Dim sendstring As String = JsonConvert.SerializeObject(postdata) Dim data As Byte() = Encoding.UTF8.GetBytes(sendstring) req.ContentLength = CLng(CInt(data.Length)) Using reqstream As Stream = req.GetRequestStream() reqstream.Write(data, 0, CInt(data.Length)) reqstream.Close() End Using Dim response As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse) Dim text As String = String.Empty Using responseStm As Stream = response.GetResponseStream() text = (New StreamReader(responseStm, Encoding.UTF8)).ReadToEnd() End Using Console.WriteLine(text) Console.ReadKey() End Sub '單記錄上傳 Private Sub 工單狀態上傳() Dim postdata As tech_info = New tech_info() With { .id = 3, .b = 4, .c = 5, .d = 6 } Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://localhost:54370/home/upload_tech_info"), HttpWebRequest) req.Method = "POST" req.ContentType = "application/json" Dim sendstring As String = JsonConvert.SerializeObject(postdata) Dim data As Byte() = Encoding.UTF8.GetBytes(sendstring) req.ContentLength = CLng(CInt(data.Length)) Using reqstream As Stream = req.GetRequestStream() reqstream.Write(data, 0, CInt(data.Length)) reqstream.Close() End Using Dim response As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse) Dim text As String = String.Empty Using responseStm As Stream = response.GetResponseStream() text = (New StreamReader(responseStm, Encoding.UTF8)).ReadToEnd() End Using Console.WriteLine(text) Console.ReadKey() End Sub End Module