【问题标题】:creating a web service with paging使用分页创建 Web 服务
【发布时间】:2010-10-07 01:58:17
【问题描述】:

我正在使用 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);

    }

这对于刚刚返回所有实体的服务非常有效,但是此方法返回分页结果。将总行数返回给客户端的最佳方式是什么?

【问题讨论】:

    标签: c# .net web-services asmx


    【解决方案1】:

    您必须创建一个自定义类型来包含计数:

    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;
    }
    

    【讨论】:

    • Web 服务在 .NET 2.0 中是否支持这种通用返回类型?
    • @John - 如果他们不这样做,原始返回值也不会起作用。
    • 谢谢,我以为这就是我必须做的,但希望我可以在 xml 中创建一个属性
    猜你喜欢
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多