1.通过查看日志,可以看出问题的原因。
2.断点调试。
3.本地测试,确保无误后,线上测试。
4.输出测试。
通过get的方式,测试接口。
// [HttpPost]public ActionResult SearchGroup(string appType, string keyWord, string userName){ GroupListModel model = new GroupListModel(); // 新建model对象 var groups = _groupService.SearchGroup(appType, keyWord); foreach (var item in groups) { model.Group.Add(new GroupModel() { GroupId = item.Id, Title = item.Title, Avatar = item.Avatar, OwnerId = item.OwnerId, IsDismiss = item.IsDismiss }); } // return Json(new { result = true, info = model }); return Json(new { result = true, info = model }, JsonRequestBehavior.AllowGet);}
通过post的方式,测试接口。
[HttpPost]public ActionResult SearchGroup(string appType, string keyWord, string userName){ GroupListModel model = new GroupListModel(); // 新建model对象 var groups = _groupService.SearchGroup(appType, keyWord); foreach (var item in groups) { model.Group.Add(new GroupModel() { GroupId = item.Id, Title = item.Title, Avatar = item.Avatar, OwnerId = item.OwnerId, IsDismiss = item.IsDismiss }); } return Json(new { result = true, info = model });}
有时候编辑器不报错,不一定就是完全正确的。
下面是接口的终端代码
[HttpPost]public ActionResult SearchGroup(string appType, string keyWord, string userName){ GroupListModel model = new GroupListModel(); // 新建model对象 var groups = _groupService.SearchGroup(appType, keyWord); foreach (var item in groups) { model.Group.Add(new GroupModel() { GroupId = item.Id, Title = item.Title, Avatar = item.Avatar, OwnerId = item.OwnerId, IsDismiss = item.IsDismiss }); } return Json(new { result = true, info = model });}
目标是获取群组数据,
首先创建一个GroupListModel。它的Group属性,对应GroupModel。GroupModel中有相应的所需的属性值。using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Ddd.Web.Models.Customer{ public partial class GroupListModel { public GroupListModel() { this.Group = new List(); } public List Group { get; set; } } public partial class GroupModel { public int GroupId { get; set; } public string Title { get; set; } public string Avatar { get; set; } public bool IsFriend { get; set; } public int OwnerId { get; set; } public bool IsDismiss { get; set; } }}
然后根据_groupService的SearchGroup方法查询数据,这里主要是一些与数据库交互的方法。各种增删改查都在这里进行。
using System;using System.Collections.Generic;using System.Linq;using Ddd.Core.Caching;using Ddd.Core.Data;using Ddd.Core.Domain.Customers;using Ddd.Services.Events;using Ddd.Core;namespace Ddd.Services.Customers{ ////// Group service /// public partial class GroupService : IGroupService { #region Constants ////// Key for caching /// private const string GROUPS_ALL_KEY = "YY.group.all"; ////// Key for caching /// ////// {0} : group ID /// private const string GROUP_BY_ID_KEY = "YY.group.id-{0}"; private const string GROUP_BY_APPTYPE_GROUPNAME_KEY = "YY.group.type-{0}.name-{1}"; ////// Key pattern to clear cache /// private const string GROUPS_PATTERN_KEY = "YY.group."; #endregion #region Fields private readonly IRepository_groupRepository; private readonly IEventPublisher _eventPublisher; private readonly ICacheManager _cacheManager; #endregion #region Ctor /// /// Ctor /// /// Cache manager /// Group repository /// Event published public GroupService(ICacheManager cacheManager, IRepositorygroupRepository, IEventPublisher eventPublisher) { this._cacheManager = cacheManager; this._groupRepository = groupRepository; this._eventPublisher = eventPublisher; } #endregion #region Methods /// /// Gets all Groups /// ///Groups public virtual IListGetAllGroups() { string key = GROUPS_ALL_KEY; return _cacheManager.Get(key, () => { var query = from a in _groupRepository.Table orderby a.Id select a; return query.ToList(); }); } /// /// Gets a Group /// /// Group identifier ///Group public virtual Group GetGroupById(int groupId) { if (groupId == 0) return null; string key = string.Format(GROUP_BY_ID_KEY, groupId); return _cacheManager.Get(key, () => _groupRepository.GetById(groupId)); } ////// Inserts a Group /// /// Group public virtual Group InsertGroup(Group group) { if (group == null) throw new ArgumentNullException("group"); _groupRepository.Insert(group); _cacheManager.RemoveByPattern(GROUPS_PATTERN_KEY); //event notification _eventPublisher.EntityInserted(group); return group; } ////// Updates the Group /// /// Group public virtual void UpdateGroup(Group group) { if (group == null) throw new ArgumentNullException("group"); _groupRepository.Update(group); _cacheManager.RemoveByPattern(GROUPS_PATTERN_KEY); //event notification _eventPublisher.EntityUpdated(group); } ////// Deletes a Group /// /// Group public virtual void DeleteGroup(Group group) { if (group == null) throw new ArgumentNullException("group"); _groupRepository.Delete(group); _cacheManager.RemoveByPattern(GROUPS_PATTERN_KEY); //event notification _eventPublisher.EntityDeleted(group); } public virtual Group GetGroupBySysnameAndGroupName(string sysName, string groupName) { if (string.IsNullOrEmpty(sysName)||string.IsNullOrEmpty(groupName)) return null; string key = string.Format(GROUP_BY_APPTYPE_GROUPNAME_KEY, sysName,groupName); return _cacheManager.Get(key, () => { var query = from g in _groupRepository.Table where g.SystemName == sysName && g.Title == groupName && !g.IsDismiss select g; return query.FirstOrDefault(); }); } ////// 获取所有的朋友圈列表 /// /// /// ///public virtual IPagedList GetAllGroups(int pageIndex = 0, int pageSize = int.MaxValue) { var query = from g in _groupRepository.Table where !g.IsDismiss orderby g.Id descending select g; return new PagedList (query, pageIndex, pageSize); } public virtual List SearchGroup(string appType, string keyWord) { var query = from x in _groupRepository.Table where x.SystemName == appType && x.Title.Contains(keyWord) && x.IsDismiss == false orderby x.Id descending select x; // 查询群名包含keyWord的数据 return query.ToList(); } #endregion }}
获取groups数据之后,将其赋值到model中去,
foreach (var item in groups){ model.Group.Add(new GroupModel() { GroupId = item.Id, Title = item.Title, Avatar = item.Avatar, OwnerId = item.OwnerId, IsDismiss = item.IsDismiss });}
最后通过Json输出,
return Json(new { result = true, info = model });
编译好之后,将下面的dll上传发布,测试。
![422101-20170516152456900-1448867280.png](https://images2015.cnblogs.com/blog/422101/201705/422101-20170516152456900-1448867280.png)
小结;实践出真知,你们看了也没用,自己多练练吧。