This code example shows how to attach data from a local Windows 10 client to an Asp.Net Core Web Api web server
A local Windows 10 UWP application sends data as a json format to the web api interface of the web server. There, the data is added to the SQL database
Shown is the HttpPost method for creating new data.
Functionality:
The client does the following:
Load data from SQlite local database and transfer it to Json. Then with a HttpClient and HttpPost send to the web server
The server does the following:
Under the HttpPost method, he takes over the request and loads the data from the request body. The data is transferred from Json to a record and stored.
Example code
Code in C #
Client = UWP App application in Windows 10
Server = Asp.Net Core MVC Api
Json, HttpPut, REST, Web Api, Sqlite, and SQL Server
API client
UWP client
SQLite, Json
HttpClient client = await Create_HttpClient_with_UserToken(); string IDDetail = "123abc"; //< read webApi > //*httpPut /api/projects string sUrl_Api_Create = app_settings._Api_Server + "/api/projects"; HttpResponseMessage httpResponseMessage = null; try { //< create Upload_Content > JsonObject jsonObject = new JsonObject(); jsonObject["IDDetail"] = JsonValue.CreateStringValue(IDDetail); jsonObject["Title"] = JsonValue.CreateStringValue("this is a Title"); StringContent string_to_Upload_Content = new StringContent(jsonObject.Stringify()); //</ create Upload_Content >
//< upload > //httpPost=Create httpResponseMessage = await client.PostAsync(sUrl_Api_Create, string_to_Upload_Content ); //</ upload > ..
|
Screenshot when sending the data
Shows: The data is inserted from the SQLite database into the Json Object and sent
Web Server
Asp.Net Core Web API
//HttpPost URL=/api/projects //*Post=Create, Put=Update [HttpPost] public async Task<ActionResult> Create() { //-------------< Create() >------------- .. //--< Read UploadData >-- StreamReader reader = new StreamReader(Request.Body); JObject jsonData = JObject.Parse(reader.ReadToEnd()); string IDDetail = jsonData.GetValue("IDDetail").ToString(); string sTitle = jsonData.GetValue("Title").ToString(); //--</ Read UploadData >--
//< Create Data > ProjectModel project = new ProjectModel();
project.IDOwner = IDCurrent_User; project.Title = sTitle; project.DtCreated = DateTime.Now;
//</ Create Data >
//< add recordset > _dbContext.tbl_Projects.Add(project); await _dbContext.SaveChangesAsync(true); //</ add recordset >
//< out > //*output to client return Ok(); //</ out > //-------------</ Create() >------------- }
|
ScreenShot on the web server Webapi when the data arrives.
The screenshot of the debugger shows that the data from the Request.Body can be read in a Json-Object and read there via key-pair
Complete code
Example, C #
Complete code of the transmission method in UWP
private async Task<bool> Api_Add_Projects() { //--------< Api_Add_Projects() >-------- if (optStop.IsChecked == true) return false; fx_Log("--< API:Api_Add_Projects >--");
//---------------< read_API_Data() >---------------
HttpClient client = await Create_HttpClient_with_UserToken();
//----< Check_to_local >---- //< find record > string sSQL = "SELECT * FROM tbl_Details WHERE [IDProject_on_Server] IS NULL"; SqliteDataReader dataReader = clsDB.Get_DataReader(sSQL); if (dataReader.HasRows == true) { while (dataReader.Read()) { //---< NoMatch: Delete on Server >--- string IDDetail = dataReader["IDDetail"].ToString(); fx_Log("add Project IDDetail:" + IDDetail); //</ nomatch >
//< read webApi > //*httpPut /api/projects string sUrl_Api_Create = app_settings._Api_Server + "/api/projects"; HttpResponseMessage httpResponseMessage = null; try { //< create Upload_Content > JsonObject jsonObject = new JsonObject(); jsonObject["IDDetail"] = JsonValue.CreateStringValue(IDDetail); jsonObject["Title"] = JsonValue.CreateStringValue("this is a Title"); StringContent string_to_Upload_Content = new StringContent(jsonObject.Stringify()); //</ create Upload_Content >
//< upload > //httpPost=Create httpResponseMessage = await client.PostAsync(sUrl_Api_Create, string_to_Upload_Content ); //</ upload >
//< result > var NewID = httpResponseMessage.Content.ToString(); //</ result >
fx_Log("ok.Created "); } catch (Exception ex) {
fx_Log("Error Delete byWebsite: " + ex.Message); //return; } //</ read webApi > //---</ NoMatch: Delete on Server >--- } }
//----</ @Loop: Projects_on_Server >---- //----</ get Projects-List >---- //---------------</ read_API_Data() >---------------
fx_Log("--</ API:Api_Add_Projects >--"); fx_Log("");
return true;
//--------</ Api_Add_Projects() >-------- }
|
Complete code of the web API interface on the server
//HttpPost URL=/api/projects //*Post=Create, Put=Update [HttpPost] public async Task<ActionResult> Create() { //-------------< Create() >------------- //< get UserClaim Info > //*get User from Token var userClaim_in_Token = HttpContext.User.Claims.Where(c => c.Type == ClaimsIdentity.DefaultNameClaimType).FirstOrDefault(); //User as Name if (userClaim_in_Token == null) { return null; } string sEmail = userClaim_in_Token.Value; //</ get UserClaim Info >
//< check user > long IDCurrent_User = await Get_UserID(sEmail); //</ check user >
//--< Read UploadData >-- StreamReader reader = new StreamReader(Request.Body); JObject jsonData = JObject.Parse(reader.ReadToEnd()); string IDDetail = jsonData.GetValue("IDDetail").ToString(); string sTitle = jsonData.GetValue("Title").ToString(); //--</ Read UploadData >--
//< Create Data > ProjectModel project = new ProjectModel();
project.IDOwner = IDCurrent_User; project.Title = sTitle; project.DtCreated = DateTime.Now; //</ Create Data >
//< add recordset > _dbContext.tbl_Projects.Add(project); //</ add recordset >
//< save sqlserver > await _dbContext.SaveChangesAsync(true); //</ save sqlserver >
//< out > //*output to client return Ok(); //</ out > //-------------</ Create() >------------- }
|