mvc5 webapi 编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using SqlSugar; using Models; using WebApplication.Dao; using System.Text; namespace WebApplication.Controllers { /// <summary> /// 分类接口 /// </summary> public class classificationController : ApiController { /// <summary> /// 获取分类列表(条件查询,分页) http://192.168.2.177:1222/api/classification/GetclassificationListPage?pageIndex=1&pageSize=10&status=2&title= /// </summary> /// <param name="pageIndex">分页索引</param> /// <param name="pageSize">分页大小</param> /// <param name="status">状态:0:下线 1:上线 -1:失效,默认传入2获取所有数据</param> /// <param name="title"></param> /// <returns></returns> [Route( "api/classification/GetclassificationListPage" )] [HttpGet] public string GetclassificationListPage( int pageIndex, int pageSize, int status, string title) { try { using ( var db = SugarDao.GetInstance()) { var qable = db.Queryable<tb_classification>(); var dataCountTable = db.Queryable<tb_classification>(); if (status != 2) { qable = qable.Where(i => i.status == status); dataCountTable = dataCountTable.Where(i => i.status == status); } if (status == 2) { dataCountTable = dataCountTable.Where(i => i.status != status); } if (! string .IsNullOrEmpty(title)) { qable = qable.Where(i => i.title.Contains(title)); dataCountTable = dataCountTable.Where(i => i.title.Contains(title)); } var data = qable.OrderBy(it => it.createtime, OrderByType.Asc).ToPageList(pageIndex, pageSize); var dataCount = dataCountTable.ToList().Count; if (data.Count > 0) { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "获取数据成功" , Data = data, Count = dataCount }); } else { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "获取数据失败" , Data = data, Count = dataCount }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "获取数据失败,原因为:" + ex.Message }); } } /// <summary> /// 获取分类数据,不分页 http://192.168.2.177:1222/api/classification/GetclassificationList?status=2&title= /// </summary> /// <param name="status"></param> /// <param name="title"></param> /// <returns></returns> [Route( "api/classification/GetclassificationList" )] [HttpGet] public string GetclassificationList( int status, string title) { try { using ( var db = SugarDao.GetInstance()) { var qable = db.Queryable<tb_classification>(); if (status != 2) { qable = qable.Where(i => i.status == status); } if (! string .IsNullOrEmpty(title)) { qable = qable.Where(i => i.title.Contains(title)); } var data = qable.OrderBy(it => it.createtime, OrderByType.Asc).ToList(); var dataCount = db.Queryable<tb_classification>().Where(it => it.status != status).ToList().Count; if (data.Count > 0) { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "获取数据成功" , Data = data, Count = dataCount }); } else { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "获取数据失败" , Data = data, Count = dataCount }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "获取数据失败,原因为:" + ex.Message }); } } /// <summary> /// 根据id得到对象 http://192.168.2.177:1222/api/classification/GetclassificationModel?id=1 /// </summary> /// <param name="id"></param> /// <returns></returns> [Route( "api/classification/GetclassificationModel" )] [HttpGet, HttpPost] public string GetclassificationModel( int id) { try { using ( var db = SugarDao.GetInstance()) { if (id != 0) { var classification = db.Queryable<tb_classification>().Single(it => it.id == id); if (classification != null ) { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "获取数据成功" , Data = classification }); } else { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "获取数据失败" , Data = classification }); } } else { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "获取数据失败,无法找到id为:" + id + "的数据" , }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "获取数据失败,原因为:" + ex.Message }); } } /// <summary> /// 新增分类 http://192.168.2.177:1222/api/classification/Postclassification /// </summary> /// <param name="classification"></param> [Route( "api/classification/Postclassification" )] [HttpPost] public string Postclassification(tb_classification classification) { try { classification.createtime = DateTime.Parse(DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss" )); using ( var db = SugarDao.GetInstance()) { object result = db.Insert<tb_classification>(classification); if (Convert.ToInt64(result) > 0) { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "添加成功" }); } else { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "添加失败" }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "添加失败,原因为:" + ex.Message }); } } /// <summary> /// 修改分类 /// </summary> /// <param name="id"></param> /// <param name="value"></param> [Route( "api/classification/Putclassification" )] [HttpPost] public string Putclassification(tb_classification classification) { try { classification.createtime = DateTime.Parse(DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss" )); using ( var db = SugarDao.GetInstance()) { bool result = db.Update<tb_classification>(classification, it => it.id == classification.id); if (result == true ) { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "更新成功" }); } else { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "更新失败" }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "更新失败,原因为:" + ex.Message }); } } /// <summary> /// 删除 http://192.168.2.177:1222/api/classification/Deleteclassification?ids=4,5 /// </summary> /// <param name="id"></param> /// <returns></returns> [Route( "api/classification/Deleteclassification" )] [HttpGet, HttpPost] public string Deleteclassification( string ids) { try { string [] input = ids.Split( ',' ); int [] output = Array.ConvertAll< string , int >(input, delegate ( string s) { return int .Parse(s); }); using ( var db = SugarDao.GetInstance()) { bool result = db.Delete<tb_classification, int >(output); if (result == true ) { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "删除成功" }); } else { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "删除失败" }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "删除失败,原因为:" + ex.Message }); } } /// <summary> /// 更新状态 http://192.168.2.177:1222/api/classification/UpdateStatus?status=0&ids=2,7 /// </summary> /// <param name="status">状态:0:下线 1:上线 -1:失效 </param> /// <param name="ids"></param> /// <returns></returns> [Route( "api/classification/UpdateStatus" )] [HttpGet] public string UpdateStatus( int status, string ids) { try { string [] input = ids.Split( ',' ); int [] output = Array.ConvertAll< string , int >(input, delegate ( string s) { return int .Parse(s); }); using ( var db = SugarDao.GetInstance()) { bool result = db.Update<tb_classification, int >( new { status = status }, output); if (result == true ) { return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "状态更改成功" }); } else { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "状态更改失败" }); } } } catch (Exception ex) { return SqlSugar.JsonConverter.Serialize( new { Result = 0, Msg = "状态更改失败,原因为:" + ex.Message }); } } string result = string .Empty; string ChildResult = string .Empty; string temp = string .Empty; /// <summary> /// 分类树形结构 http://192.168.2.177:1222/api/classification/GetclassificationTree /// </summary> /// <returns></returns> [Route( "api/classification/GetclassificationTree" )] [HttpGet] public string GetclassificationTree() { WebApplication.Controllers.TreeMethod tm = new TreeMethod(); // 找到所有的父节点 List<TreeEntity> treeList1 = tm.findAllParents(); if (treeList1 != null ) { for ( int i = 0; i < treeList1.Count; i++) { TreeEntity tree = treeList1[i]; // 拼接父节点 //result += "|--" + tree.name; result += "{id:" +tree.id+ ",pId:" +tree.pid+ ",name:'" +tree.name+ "'}," ; // 绑定孩子 ChildResult = tm.BindChildByParent(tree.id, "" ).TrimEnd( ',' ); temp = result + ChildResult; } } else { temp += "没有数据!" ; } result = "[" + temp.TrimEnd( ',' )+ "]" ; return SqlSugar.JsonConverter.Serialize( new { Result = 1, Msg = "获取数据成功" ,Data= temp }); } } public class TreeEntity { public string id { get ; set ; } public string name { get ; set ; } public string pid { get ; set ; } } internal class TreeMethod { /// <summary> /// 找到所有的父节点 /// </summary> /// <returns></returns> public List<TreeEntity> findAllParents() { List<TreeEntity> treeList = new List<TreeEntity>(); using ( var db = SugarDao.GetInstance()) { var list = db.Queryable<tb_classification>().Where(it => it.pid == 0 && it.status==1).ToList(); if (list.Count > 0) { for ( int i = 0; i < list.Count; i++) { TreeEntity myTree = new TreeEntity(); myTree.id = list[i].id.ToString(); myTree.name = list[i].title; myTree.pid = list[i].pid.ToString(); treeList.Add(myTree); } } } return treeList; } /// <summary> /// 根据父节点找到所有的子节点 /// </summary> /// <param name="pid"></param> /// <returns></returns> public List<TreeEntity> findChildByPid( string pid) { int p_id = Convert.ToInt32(pid); List<TreeEntity> treeList = new List<TreeEntity>(); using ( var db = SugarDao.GetInstance()) { var list = db.Queryable<tb_classification>().Where(it => it.pid == p_id&&it.status==1).ToList(); if (list.Count > 0) { for ( int i = 0; i < list.Count; i++) { TreeEntity myTree = new TreeEntity(); myTree.id = list[i].id.ToString(); myTree.name = list[i].title; myTree.pid = list[i].pid.ToString(); treeList.Add(myTree); } } } return treeList; } /// <summary> /// 查看是否存在子节点 /// </summary> /// <param name="pid"></param> /// <returns></returns> public bool HasChild( string pid) { int p_id = Convert.ToInt32(pid); int count = 0; bool flag = false ; using ( var db = SugarDao.GetInstance()) { var list = db.Queryable<tb_classification>().Where(it => it.pid == p_id&it.status==1).ToList(); for ( int i = 0; i < list.Count; i++) { count++; } if (count > 0) { flag = true ; } } return flag; } string Tree = string .Empty; /// <summary> /// 使用递归拼接父节点的子节点 /// </summary> /// <param name="pid"></param> /// <param name="prefix"></param> public string BindChildByParent( string pid, string prefix) { if ( this .HasChild(pid)) { // 得到当前父节点下的所有孩子 List<TreeEntity> list = this .findChildByPid(pid); // 循环拼接当前父节点下的孩子 for ( int i = 0; i < list.Count; i++) { //Tree += "|----" + prefix + list[i].name; Tree += "{id:" + list[i].id + ",pId:" + list[i].pid + ",name:'" + list[i].name + "'}," ; if ( this .HasChild(list[i].id)) { this .BindChildByParent(list[i].id, "--" ); } } } return Tree; } } } js调用: <div class = "jumbotron" > <h1>ASP.NET</h1> <p class = "lead" >ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS, and JavaScript.</p> <p><a href= "http://asp.net" class = "btn btn-primary btn-lg" >Learn more »</a></p> </div> <div class = "row" > <div class = "col-md-4" > <h2>Getting started</h2> <p>ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.</p> <p><a class = "btn btn-default" href= "http://go.microsoft.com/fwlink/?LinkId=301870" >Learn more »</a></p> </div> <div class = "col-md-4" > <h2>Get more libraries</h2> <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p> <p><a class = "btn btn-default" href= "http://go.microsoft.com/fwlink/?LinkId=301871" >Learn more »</a></p> </div> <div class = "col-md-4" > <h2>Web Hosting</h2> <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p> <p><a class = "btn btn-default" href= "http://go.microsoft.com/fwlink/?LinkId=301872" >Learn more »</a></p> </div> </div> <script src= "~/Scripts/jquery-1.10.2.js" ></script> <script> $(function () { //var json = "{\"pid\": 0, \"title\": \"123\", \"introduction\":\"321\"}"; //var jsonjson = JSON.parse(json); //$.ajax({ // type: "post", // dataType: "json", // url: " http://192.168.2.177:1222/api/classification/Postclassification", // data:{"str":"jsonjson"}, // success: function (data) { // console.log(data); // } //}); }); function OK() { $.ajax({ type: "post" , url: "http://192.168.2.177:1222/api/classification/Postclassification" , data: { pid: 1, title: '123哈哈1' , introduction: '介绍1' }, success: function (data) { console.log(data); } }); } function update() { $.ajax({ type: "post" , url: "http://192.168.2.177:1222/api/classification/Putclassification" , data: { id: 11, pid: 1, title: '更新一下' , introduction: '更新一下介绍吧' }, success: function (data) { console.log(data); } }); } </script> <div> <h1>新增操作</h1> pid:<input type= "text" id= "txtPid" /><br /> title:<input type= "text" id= "txtTitle" /><br /> introduction:<input type= "text" id= "txtintroduction" /><br /> <input type= "button" value= "提交" onclick= "OK()" /> </div> <div> <h1>修改操作</h1> pid:<input type= "text" id= "txtPid" /><br /> title:<input type= "text" id= "txtTitle" /><br /> introduction:<input type= "text" id= "txtintroduction" /><br /> <input type= "button" value= "提交" onclick= "update()" /> </div> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探