- A+
所属分类:.NET技术
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=".logsstdout" 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