1、創建WEBAPI 1 using Dapper; 2 using MesErp.Models; 3 using Microsoft.AspNetCore.Mvc; 4 using Microsoft.Extensions.Configuration; 5 using Newtonsoft.Jso ...
1、創建WEBAPI
1 using Dapper; 2 using MesErp.Models; 3 using Microsoft.AspNetCore.Mvc; 4 using Microsoft.Extensions.Configuration; 5 using Newtonsoft.Json; 6 using System; 7 using System.Collections.Generic; 8 using System.Data; 9 using System.Data.Common; 10 using System.Data.SqlClient; 11 using System.Linq; 12 using System.Runtime.Intrinsics.Arm; 13 using System.Security.Cryptography; 14 using System.Threading.Tasks; 15 using System.Xml.Linq; 16 namespace MesErp.Controllers 17 { 18 /// <summary> 19 /// ERP-MES基礎數據同步" 20 /// </summary> 21 [Route("[controller]")] 22 [ApiController] 23 public class ErpMesBasicController : ControllerBase 24 { 25 private string ConnectionStr() 26 { 27 var appSettingsJson = AppSettingsJson.GetAppSettings(); 28 string connectionString2 = appSettingsJson.GetSection("ConnectionStrings")["DefaultConnection"];//鏈接資料庫字元串 29 return connectionString2; 30 } 31 /// <summary> 32 /// 庫存組織新增/更新 33 /// </summary> 34 /// <returns></returns> 35 [HttpPost("AddWorkcenter")] 36 public async Task< ErpMesRts> AddWorkcenter([FromBody] WorkcenterModel workcenters) 37 { 38 int rst = -1; 39 ErpMesRts rstsate = new ErpMesRts(); 40 41 string connectionString = ConnectionStr();//鏈接字元串 42 DynamicParameters paras = new DynamicParameters(); 43 string Ctworkter= JsonConvert.SerializeObject(workcenters); 44 paras.Add("@JosnName", Ctworkter); 45 // paras.Add("@sum", 0, DbType.Int32, ParameterDirection.Output);// 指明是輸出參數,這裡為指明參數類型大小 46 using (IDbConnection connection = new SqlConnection(connectionString)) 47 { 48 try 49 { 50 rst = await connection.ExecuteAsync("proc_ERP_MesAddWorkcenter", paras, commandType: CommandType.StoredProcedure); 51 } 52 catch(Exception e) 53 { 54 rstsate.msg=e.Message.ToString()+"資料庫異常"; 55 56 } 57 // 執行存儲過程 58 // rst = await connection.ExecuteAsync("proc_AddSysUser01", paras , commandType: CommandType.StoredProcedure); 59 60 } 61 if (rst > 0) 62 { 63 rstsate.status = 200; 64 rstsate.msg = "成功"; 65 } 66 67 68 //"{status: = " + code + ", msg = " + msg + ", data = " + null "}"; 69 return rstsate; 70 //rst 返回1為成功 71 // return Json(new { code = 200, msg = "ok" }); 72 } 73 /// <summary> 74 /// 人員主數據新增/更新 75 /// </summary> 76 /// <returns></returns> 77 [HttpPost("AddPersonInfo")] 78 public async Task<ErpMesRts> AddPersonInfo([FromBody] PersonInfoModel personInfos) 79 { 80 int rst = -1; 81 ErpMesRts rstsate = new ErpMesRts(); 82 83 string connectionString = ConnectionStr();//鏈接字元串 84 DynamicParameters paras = new DynamicParameters(); 85 string Ctworkter = JsonConvert.SerializeObject(personInfos); 86 paras.Add("@JosnName", Ctworkter); 87 // paras.Add("@sum", 0, DbType.Int32, ParameterDirection.Output);// 指明是輸出參數,這裡為指明參數類型大小 88 using (IDbConnection connection = new SqlConnection(connectionString)) 89 { 90 try 91 { 92 rst = await connection.ExecuteAsync("proc_ERP_MesAddPersonInfo", paras, commandType: CommandType.StoredProcedure); 93 } 94 catch (Exception e) 95 { 96 rstsate.msg = e.Message.ToString() + "資料庫異常"; 97 98 } 99 // 執行存儲過程 100 // rst = await connection.ExecuteAsync("proc_AddSysUser01", paras , commandType: CommandType.StoredProcedure); 101 102 } 103 if (rst > 0) 104 { 105 rstsate.status = 200; 106 rstsate.msg = "成功"; 107 } 108 109 110 //"{status: = " + code + ", msg = " + msg + ", data = " + null "}"; 111 return rstsate; 112 //rst 返回1為成功 113 // return Json(new { code = 200, msg = "ok" }); 114 } 115 /// <summary> 116 /// 業務組新增/更新 117 /// </summary> 118 /// <returns></returns> 119 [HttpPost("AddBusinessGroup")] 120 public async Task<ErpMesRts> AddBusinessGroup([FromBody] BusinessGroupModel businessGroups) 121 { 122 int rst = -1; 123 ErpMesRts rstsate = new ErpMesRts(); 124 125 string connectionString = ConnectionStr();//鏈接字元串 126 DynamicParameters paras = new DynamicParameters(); 127 string Ctworkter = JsonConvert.SerializeObject(businessGroups); 128 paras.Add("@JosnName", Ctworkter); 129 // paras.Add("@sum", 0, DbType.Int32, ParameterDirection.Output);// 指明是輸出參數,這裡為指明參數類型大小 130 using (IDbConnection connection = new SqlConnection(connectionString)) 131 { 132 try 133 { 134 rst = await connection.ExecuteAsync("proc_ERP_MesAddBusinessGroup", paras, commandType: CommandType.StoredProcedure); 135 } 136 catch (Exception e) 137 { 138 rstsate.msg = e.Message.ToString() + "資料庫異常"; 139 140 } 141 // 執行存儲過程 142 // rst = await connection.ExecuteAsync("proc_AddSysUser01", paras , commandType: CommandType.StoredProcedure); 143 144 } 145 if (rst > 0) 146 { 147 rstsate.status = 200; 148 rstsate.msg = "成功"; 149 } 150 151 152 //"{status: = " + code + ", msg = " + msg + ", data = " + null "}"; 153 return rstsate; 154 //rst 返回1為成功 155 // return Json(new { code = 200, msg = "ok" }); 156 } 157 /// <summary> 158 /// 組織主數據/部門主數據新增/更新 159 /// </summary> 160 /// <returns></returns> 161 [HttpPost("AddDivision")] 162 public async Task<ErpMesRts> AddDivision([FromBody] DivisionModel divisions) 163 { 164 int rst = -1; 165 ErpMesRts rstsate = new ErpMesRts(); 166 167 string connectionString = ConnectionStr();//鏈接字元串 168 DynamicParameters paras = new DynamicParameters(); 169 string Ctworkter = JsonConvert.SerializeObject(divisions); 170 paras.Add("@JosnName", Ctworkter); 171 // paras.Add("@sum", 0, DbType.Int32, ParameterDirection.Output);// 指明是輸出參數,這裡為指明參數類型大小 172 using (IDbConnection connection = new SqlConnection(connectionString)) 173 { 174 try 175 { 176 rst = await connection.ExecuteAsync("proc_ERP_MesAddDivision", paras, commandType: CommandType.StoredProcedure); 177 } 178 catch (Exception e) 179 { 180 rstsate.msg = e.Message.ToString() + "資料庫異常"; 181 182 } 183 // 執行存儲過程 184 // rst = await connection.ExecuteAsync("proc_AddSysUser01", paras , commandType: CommandType.StoredProcedure); 185 186 } 187 if (rst > 0) 188 { 189 rstsate.status = 200; 190 rstsate.msg = "成功"; 191 } 192 193 194 //"{status: = " + code + ", msg = " + msg + ", data = " + null "}"; 195 return rstsate; 196 //rst 返回1為成功 197 // return Json(new { code = 200, msg = "ok" }); 198 } 199 /// <summary> 200 /// 物料類型新增/更新 201 /// </summary> 202 /// <returns></returns> 203 [HttpPost("AddProductType")] 204 public async Task<ErpMesRts> AddProductType([FromBody] ProductTypeModel productTypes) 205 { 206 int rst = -1; 207 ErpMesRts rstsate = new ErpMesRts(); 208 209 string connectionString = ConnectionStr();//鏈接字元串 210 DynamicParameters paras = new DynamicParameters(); 211 string Ctworkter = JsonConvert.SerializeObject(productTypes); 212 paras.Add("@JosnName", Ctworkter); 213 // paras.Add("@sum", 0, DbType.Int32, ParameterDirection.Output);// 指明是輸出參數,這裡為指明參數類型大小 214 using (IDbConnection connection = new SqlConnection(connectionString)) 215 { 216 try 217 { 218 rst = await connection.ExecuteAsync("proc_ERP_MesAddProductType", paras, commandType: CommandType.StoredProcedure); 219 } 220 catch (Exception e) 221 { 222 rstsate.msg = e.Message.ToString() + "資料庫異常"; 223 224 } 225 // 執行存儲過程 226 // rst = await connection.ExecuteAsync("proc_AddSysUser01", paras , commandType: CommandType.StoredProcedure); 227 228 } 229 if (rst > 0) 230 { 231 rstsate.status = 200; 232 rstsate.msg = "成功"; 233 } 234 235 236 //"{status: = " + code + ", msg = " + msg + ", data = " + null "}"; 237 return rstsate; 238 //rst 返回1為成功 239 // return Json(new { code = 200, msg = "ok" }); 240 } 241 /// <summary> 242 /// 物料主數據新增/更新 243 /// </summary> 244 /// <returns></returns> 245 [HttpPost("AddProduct")] 246 public async Task<ErpMesRts> AddProduct([FromBody] ProductModel products) 247 { 248 int rst = -1; 249 ErpMesRts rstsate = new ErpMesRts(); 250 251 string connectionString = ConnectionStr();//鏈接字元串 252 DynamicParameters paras = new DynamicParameters(); 253 string Ctworkter = JsonConvert.SerializeObject(products); 254 paras.Add("@JosnName", Ctworkter); 255 // paras.Add("@sum", 0, DbType.Int32, ParameterDirection.Output);// 指明是輸出參數,這裡為指明參數類型大小 256 using (IDbConnection connection = new SqlConnection(connectionString)) 257 { 258 try 259 { 260 rst = await connection.ExecuteAsync("proc_ERP_MesAddProduct", paras, commandType: CommandType.StoredProcedure); 261 } 262 catch (Exception e) 263 { 264 rstsate.msg = e.Message.ToString() + "資料庫異常"; 265 266 } 267 // 執行存儲過程 268 // rst = await connection.ExecuteAsync("proc_AddSysUser01", paras , commandType: CommandType.StoredProcedure); 269 270 } 271 if (rst > 0) 272 { 273 rstsate.status = 200; 274 rstsate.msg = "成功"; 275 } 276 277 278 //"{status: = " + code + ", msg = " + msg + ", data = " + null "}"; 279 return rstsate; 280 //rst 返回1為成功 281 // return Json(new { code = 200, msg = "ok" }); 282 } 283 284 285 286 287 288 /* 289 /// <summary> 290 /// 庫存組織更新 291 /// </summary> 292 /// <param name="testModel">接收對象</param> 293 /// <returns></returns> 294 [HttpPost("UpdatWorkcenter")] 295 public async Task<string> updateBlog([FromBody] WorkcenterModel testModel) 296 { 297 //這個就代表的你第二層,懂弄?很多層都是一樣的 298 299 300 int rst = -1; 301 int code = 500; 302 string msg = "失敗"; 303 string connectionString = ConnectionStr();//鏈接字元串 304 using (IDbConnection connection = new SqlConnection(connectionString)) 305 { try 306 { // 執行存儲過程 307 rst = await connection.ExecuteAsync(" UPDATE Workcenter SET WorkcenterDescription=isnull(@WorkcenterDescription,WorkcenterDescription),IsHB=@IsHB, SiteId=@SiteId,SiteName = @SiteName,IsClosed = @IsClosed WHERE WorkcenterName = @WorkcenterName ", testModel.workcenters); 308 } 309 catch (Exception e) 310 { 311 msg = e.Message.ToString() + "資料庫異常"; 312 313 } 314 } 315 if (rst > 0) 316 { 317 code = 200; 318 msg = "成功"; 319 } 320 return "{code = " + code + ", msg = " + msg + "}"; 321 } 322 323 */ 324 325 } 326 }View Code
用restful 風格創建介面
鏈接字元串
2、webapi發佈
發佈路徑
發佈時選擇空文件
選擇路徑
發佈完成示例圖
註意編輯介面是會勾選XML輸出
這個路徑打包編譯時是不會自動編譯過去的
3、發佈到IIS上
創建應用池時(創建無托管、集成)
部署伺服器環境需要安裝三個文件
https://dotnet.microsoft.com/zh-cn/download/dotnet/5.0環境下載、也可參照其他鏈接https://blog.csdn.net/xiaochenXIHUA/article/details/118027591
環境安裝後需要將文件發佈IIS上
同時配置將XML文件考到發佈文件裡面
配製web.config加下麵節點
配置代碼
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <location path="." inheritInChildApplications="false"> 4 <system.webServer> 5 <handlers> 6 <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> 7 </handlers> 8 <aspNetCore processPath="dotnet" arguments=".\MesErp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" > 9 <environmentVariables> 10 <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> 11 </environmentVariables> 12 </aspNetCore> 13 </system.webServer> 14 </location> 15 </configuration> 16 <!--ProjectGuid: f7e462b4-210d-455c-b73f-b1a6fcbfb0a8-->View Code
運行webapi