使用分页创建Web服务

时间:2010-10-07 01:58:17

标签: c# .net web-services asmx

我正在使用c#在asp.net 2.0中创建一个Web服务,并且有一个web方法,如下所示:

 [WebMethod()]
    public List<Comment> GetYourSayComments(int pageNumber, int pageSize, int commentTopicId)
    {
        CommentManager cm = new CommentManager();
        return cm.GetYourSayComments(pageNumber, pageSize, commentTopicId, true);

    }

这对刚刚返回所有实体的服务起了作用,但是这个方法会返回分页结果。将总行数返回给客户端的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

您必须创建自定义类型以包含计数:

public class EnvelopeWithCount<T>
{
    public T Value { get; set; }
    public int RowCount { get; set; }
}

然后您的网络服务将返回新类型:

[WebMethod]
public EnvelopeWithCount<List<Comment>> GetYourSayComments(int pageNumber,
                                                           int pageSize,
                                                           int commentTopicId)
{
    CommentManager cm = new CommentManager();
    Envelope<List<Comment>> retrunVal = new Envelope<List<Comment>>();
    returnVal.Value = cm.GetYourSayComments(pageNumber,
                                            pageSize,
                                            commentTopicId,
                                            true);
    // Get the count of all rows however you need
    returnVal.RowCount = cm.GetYourSayComments(true).Count();

    return returnVal;
}