博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.Net接口调试与案例
阅读量:7079 次
发布时间:2019-06-28

本文共 8233 字,大约阅读时间需要 27 分钟。

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, IRepository
groupRepository, IEventPublisher eventPublisher) { this._cacheManager = cacheManager; this._groupRepository = groupRepository; this._eventPublisher = eventPublisher; } #endregion #region Methods ///
/// Gets all Groups /// ///
Groups
public virtual IList
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(); }); } ///
/// 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

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

转载地址:http://chdml.baihongyu.com/

你可能感兴趣的文章
Android 8.0 系统和API的变化
查看>>
Git 多人协作开发流程
查看>>
js 时间对象的常规操作
查看>>
Centos 7 Yum方式安装Mongdb 3.4
查看>>
遇见大数据可视化 : 【云图】让数据可见
查看>>
Mac Docker 创建第一个Django 应用,Part 1
查看>>
zendAPI 的 CMake 参数详解
查看>>
【201天】黑马程序员27天视频学习笔记【Day18复习脑图】
查看>>
vue+webpack搭建单文件应用和多文件应用webpack.config.js的写法区别
查看>>
leetcode82. Remove Duplicates from Sorted List II
查看>>
简单学习node微信开发
查看>>
使用vue实现tab操作
查看>>
Yii2实现ActiveForm ajax提交
查看>>
【译】State and Lifecycle (State和生命周期)
查看>>
C接口与实现---之三
查看>>
浅析React之事件系统(一)
查看>>
解密新一代Java JIT编译器Graal
查看>>
HTTP协议:看个新闻原来这么麻烦
查看>>
服务平台化,知乎 HBase 实践
查看>>
2015年度最流行PHP框架调查结果出炉,Laravel居首
查看>>