.Net接口调试与案例
生活随笔
收集整理的這篇文章主要介紹了
.Net接口调试与案例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.通過查看日志,可以看出問題的原因。
2.斷點調(diào)試。
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 });
}
目標(biāo)是獲取群組數(shù)據(jù),
首先創(chuàng)建一個GroupListModel。它的Group屬性,對應(yīng)GroupModel。GroupModel中有相應(yīng)的所需的屬性值。
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; }
}
}
然后根據(jù)_groupService的SearchGroup方法查詢數(shù)據(jù),這里主要是一些與數(shù)據(jù)庫交互的方法。各種增刪改查都在這里進(jìn)行。
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; // 查詢?nèi)好琸eyWord的數(shù)據(jù)
return query.ToList();
}
#endregion
}
}
獲取groups數(shù)據(jù)之后,將其賦值到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上傳發(fā)布,測試。
小結(jié);實踐出真知,你們看了也沒用,自己多練練吧。
總結(jié)
以上是生活随笔為你收集整理的.Net接口调试与案例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中信银行信用卡现金分期电子卡是什么?怎么
- 下一篇: DataTables warning: