0
点赞
收藏
分享

微信扫一扫

Net实现erp-webapi接口案例

大南瓜鸭 2023-04-19 阅读 68

背景:致远oa项目需要封装出一个二开的物料接口;

思路:通过数据库表封装为resful+json,供sap接口调用实现oa的物料的新增与变更功能;

以下是思路截图:

1 右击解决方案,选择aspnetweb应用程序;

Net实现erp-webapi接口案例_json

2、选择empty--》webapi

Net实现erp-webapi接口案例_封装_02

3、右击controllers--》选择控制器----》webapi2控制器空;

Net实现erp-webapi接口案例_json_03

4、控制器名称取名为:oaController

Net实现erp-webapi接口案例_json_04


5、以下代码是自动获取post过来的body中的json串,这里接收后装换为json对象,做后续逻辑处理;

      Request.Content.ReadAsStreamAsync().Result.Seek(0, System.IO.SeekOrigin.Begin);

           string content = Request.Content.ReadAsStringAsync().Result;

 6、代码调用方法使用postman测试:

Net实现erp-webapi接口案例_封装_05

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);


举报

相关推荐

0 条评论