C# asp.net mvc 通过 HttpClient 访问 Web_API
程序员文章站
2022-05-23 22:12:19
//MVC 具体方法//API地址 通过 WebConfig配置 private static string apiAdds = ConfigurationManager.AppSettings["ApiAddress"]; //具体方法 public int AddSelectFlowerBll(... ......
//mvc 具体方法
//api地址 通过 webconfig配置 private static string apiadds = configurationmanager.appsettings["apiaddress"]; //具体方法 public int addselectflowerbll(string selectproduct, string productname, string productsum,int userid) { try { //非空判断 if (!string.isnullorempty(selectproduct) && !string.isnullorempty(productname) && !string.isnullorempty(productsum)) { assembleflower aflower = new assembleflower(); aflower.name = productname; aflower.pricesum = convert.toint32(productsum); //添加人为1 aflower.assembleman = userid; aflower.assembletime = datetime.now; aflower.isdel = 0; list<assemblematerial> materiallist = new list<assemblematerial>(); string[] productarry = selectproduct.split(';'); for (int i = 0; i < productarry.length - 1; i++) { string[] flowerarry = productarry[i].split(','); assemblematerial m = new assemblematerial(); m.materialid = convert.toint32(flowerarry[0]); m.materialcount = convert.toint32(flowerarry[1]); materiallist.add(m); } string[] strarray = { jsonconvert.serializeobject(aflower), jsonconvert.serializeobject(materiallist) }; #region 向后台提交数据 //创建httpclient对象 uri uri = new uri(apiadds); httpclient client = new httpclient(); client.baseaddress = uri; client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); var jsonstr = jsonconvert.serializeobject(strarray); httpcontent cont = new stringcontent(jsonstr); cont.headers.contenttype = new mediatypeheadervalue("application/json"); var returnstr = ""; httpresponsemessage resp = client.postasync("api/selectflower/assembleadd", cont).result;//post提交数据, if (resp.issuccessstatuscode) { returnstr = resp.content.readasstringasync().result; } #endregion return convert.toint32(jsonconvert.deserializeobject(returnstr)); } else { //空值 return -1; } } catch { //发生错误 return -1; throw; } }
//api [routeprefix("api/selectflower")] public class selectflowercontroller : apicontroller { /// <summary> /// 获取自选花的类型 /// </summary> /// <param name="flowertype"></param> /// <returns></returns> [httpget] [route("getselectflower")] public datatable getselectflower(string flowertype) { string sql = string.format("select * from flowermaterial where isdel=0 and flowertype=@flowertype"); sqlparameter parameter = new sqlparameter("@flowertype", sqldbtype.varchar, 200); parameter.value = flowertype; return dbhelpersql.querydatatable(sql, parameter); } [httppost] [route("assembleadd")] public string assembleadd([frombody] dynamic jsonstr) { //将前台传过来的值转化为数组 var data = jsonconvert.deserializeobject<string[]>(jsonstr.tostring()); assembleflower aflower = jsonconvert.deserializeobject<assembleflower>(data[0]); list<assemblematerial> materiallist = jsonconvert.deserializeobject<list<assemblematerial>>(data[1]); list<dbhelpersql.keyvalue> list=new list<dbhelpersql.keyvalue>(); list.add(new dbhelpersql.keyvalue() { key = "insert into assembleflower values(@name,@pricesum,@assembleman,@assembletime,0)", value = new sqlparameter[] { new sqlparameter("@name",aflower.name), new sqlparameter("@pricesum",aflower.pricesum), new sqlparameter("@assembleman",aflower.assembleman), new sqlparameter("@assembletime",aflower.assembletime), } }); foreach (var material in materiallist) { list.add(new dbhelpersql.keyvalue() { key = "insert into assemblematerial values(@materialid,@materialcount,(select top 1 id from assembleflower order by id desc))", value = new sqlparameter[] { new sqlparameter("@materialid",material.materialid), new sqlparameter("@materialcount",material.materialcount) } }); } list.add(new dbhelpersql.keyvalue() { key = " insert into mycar values(3,(select top 1 id from assembleflower order by id desc),1,@userid,@createtime,0)", value = new sqlparameter[] { new sqlparameter("@userid",aflower.assembleman), new sqlparameter("@createtime",datetime.now) } }); return dbhelpersql.executesqltranandreturn(list).tostring(); } }