在asp.net mvc中如何写webAPI及调用说明(简单版),实现跨域调用WEBAPI返回JSON数据。
工具/原料
方法/步骤
1编写实体类 public class KnowledgeLecture { public string id; public string title; public string lectureContent; public string organize; public string participant; public DateTime? lecturetime; public DateTime? creatorTime; }
2编写webapi 代码,可以发布出去,方便其他系统调用: // //获取所有的应急知识讲座 // //id、讲座标题、创建时间等的json数据 [HttpGet] public IEnumerable GetAllKnowledgeLectureInfo() {DateTime time = DateTime.Now.ToString('yyyy-MM-dd').ToDate();var data = (from s in db.FXJC_yjzh_training_knowledgeLecture.Where(s => s.deleteMark != true) select s);List Result = new List(); foreach (var o in data) { Result.Add(new KnowledgeLecture() { id = o.id, title = o.lectureTitle, lectureContent=o.lectureContent, organize=o.Sys_Organize.F_FullName, participant=o.participant, lecturetime=o.lecturetime, creatorTime = o.creatorTime, }); } return Result.OrderByDescending(s => s.creatorTime); }
3新的应用系统需要调用上面的接口,编写新的业务前端代码
注意事项
1以上就是小编带给大家的如何操作的关键所在,如果觉得本经验对你们有帮助,请给小编我进行一点小小的支持。大家也可以下面发表一下自己的看法。