.Net接口调试与案例

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<GroupModel>();
        }
        public List<GroupModel> 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
{
    /// <summary>
    /// Group service
    /// </summary>
    public partial class GroupService : IGroupService
    {
        #region Constants

        /// <summary>
        /// Key for caching
        /// </summary>
        private const string GROUPS_ALL_KEY = "YY.group.all";
        /// <summary>
        /// Key for caching
        /// </summary>
        /// <remarks>
        /// {0} : group ID
        /// </remarks>
        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}";
        /// <summary>
        /// Key pattern to clear cache
        /// </summary>
        private const string GROUPS_PATTERN_KEY = "YY.group.";
        #endregion
        
        #region Fields
        private readonly IRepository<Group> _groupRepository;
        private readonly IEventPublisher _eventPublisher;
        private readonly ICacheManager _cacheManager;
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="groupRepository">Group repository</param>
        /// <param name="eventPublisher">Event published</param>
        public GroupService(ICacheManager cacheManager,
            IRepository<Group> groupRepository,
            IEventPublisher eventPublisher)
        {
            this._cacheManager = cacheManager;
            this._groupRepository = groupRepository;
            this._eventPublisher = eventPublisher;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets all Groups
        /// </summary>
        /// <returns>Groups</returns>
        public virtual IList<Group> GetAllGroups()
        {
            string key = GROUPS_ALL_KEY;
            return _cacheManager.Get(key, () =>
            {
                var query = from a in _groupRepository.Table
                            orderby a.Id
                            select a;
                return query.ToList();
            });
        }

        /// <summary>
        /// Gets a Group 
        /// </summary>
        /// <param name="groupId">Group identifier</param>
        /// <returns>Group</returns>
        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));
        }

        /// <summary>
        /// Inserts a Group
        /// </summary>
        /// <param name="group">Group</param>
        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;
        }

        /// <summary>
        /// Updates the Group
        /// </summary>
        /// <param name="group">Group</param>
        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);
        }

        /// <summary>
        /// Deletes a Group
        /// </summary>
        /// <param name="group">Group</param>
        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();
            });
        }


        /// <summary>
        /// 获取所有的朋友圈列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public virtual IPagedList<Group> 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<Group>(query, pageIndex, pageSize);
        }

        public virtual List<Group> 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上传发布,测试。
.Net接口调试与案例

小结;实践出真知,你们看了也没用,自己多练练吧。

本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6861611.html,如需转载请自行联系原作者

上一篇:AliOS Things蓝牙协议栈及应用开发框架介绍


下一篇:MongoDB · 特性分析 · 网络性能优化