背景:致远oa项目需要封装出一个二开的物料接口;
思路:通过数据库表封装为resful+json,供sap接口调用实现oa的物料的新增与变更功能;
以下是思路截图:
1 右击解决方案,选择aspnetweb应用程序;
2、选择empty--》webapi
3、右击controllers--》选择控制器----》webapi2控制器空;
4、控制器名称取名为:oaController
5、以下代码是自动获取post过来的body中的json串,这里接收后装换为json对象,做后续逻辑处理;
Request.Content.ReadAsStreamAsync().Result.Seek(0, System.IO.SeekOrigin.Begin);
string content = Request.Content.ReadAsStringAsync().Result;
6、代码调用方法使用postman测试:
7、使用c#调用webapi的实例:
var client = new RestClient("http://localhost:5134/api/oa/AddMaterial"); client.Timeout = -1; var request = new RestRequest(Method.POST); client.UserAgent = "Apifox/1.0.0 (https://www.apifox.cn)"; request.AddHeader("Content-Type", "application/json"); request.AddHeader("Accept", "*/*"); request.AddHeader("Host", "localhost:5134"); request.AddHeader("Connection", "keep-alive"); var body = @"{ " + "\n" + @" ""MATNR"": ""200000004"", " + "\n" + @" ""MAKTX"": ""BOM接口测试-半成品"", " + "\n" + @" ""MTART"": ""Z002"", " + "\n" + @" ""MEINS"": ""EA"", " + "\n" + @" ""BISMT"": ""02.35.30C 854 949-2"", " + "\n" + @" ""LVORM"": """", " + "\n" + @" ""MAKTL"": ""30003"", " + "\n" + @" " + "\n" + @" ""MARC"": [{ " + "\n" + @" ""WERKS"": ""1011"", " + "\n" + @" ""LVORM"": """", " + "\n" + @" ""EKGRP"": ""101"" " + "\n" + @" }, " + "\n" + @" { " + "\n" + @" ""WERKS"": ""1021"", " + "\n" + @" ""LVORM"": """", " + "\n" + @" ""EKGRP"": ""101"" " + "\n" + @" } " + "\n" + @" ] " + "\n" + @"}"; request.AddParameter("application/json", body, ParameterType.RequestBody); IRestResponse response = client.Execute(request); Console.WriteLine(response.Content);