<commit_before>﻿/*
 * Authors:
 *   钟峰(<NAME>) <<EMAIL>>
 *
 * Copyright (C) 2016 Zongsoft Corporation <http://www.zongsoft.com>
 *
 * This file is part of Zongsoft.Web.
 *
 * Zongsoft.Web is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Zongsoft.Web is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Zongsoft.Web; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Filters;

namespace Zongsoft.Web.Http
{
	public class HttpPagingAttribute : ActionFilterAttribute
	{
		#region 常量定义
		private const string PAGING_PARAMETER_NAME = "paging";
		#endregion

		#region 重写方法
		public override void OnActionExecuted(HttpActionExecutedContext context)
		{
			if(context.Response.IsSuccessStatusCode)
			{
				//获取当前操作是否支持数据分页加载
				var paging = this.GetPaging(context.ActionContext.ActionArguments);

				if(paging != null)
				{
					//注意：由于数据可能未被加载，因此分页结果尚未计算；故此需要显式加载数据以驱动分页计算
					context.Response.Content.LoadIntoBufferAsync();

					if(paging.PageCount > 0)
					{
						context.Response.Content.Headers.Add("x-paging-size", paging.PageSize.ToString());
						context.Response.Content.Headers.Add("x-paging-index", paging.PageIndex.ToString());
						context.Response.Content.Headers.Add("x-paging-count", paging.PageCount.ToString());
						context.Response.Content.Headers.Add("x-result-totalCount", paging.TotalCount.ToString());
					}
				}
			}
<commit_msg>fix: 修复一个错误。 :bug: :fire:<commit_after>﻿/*
 * Authors:
 *   钟峰(<NAME>) <<EMAIL>>
 *
 * Copyright (C) 2016 Zongsoft Corporation <http://www.zongsoft.com>
 *
 * This file is part of Zongsoft.Web.
 *
 * Zongsoft.Web is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Zongsoft.Web is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Zongsoft.Web; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Filters;

namespace Zongsoft.Web.Http
{
	public class HttpPagingAttribute : ActionFilterAttribute
	{
		#region 常量定义
		private const string PAGING_PARAMETER_NAME = "paging";
		#endregion

		#region 重写方法
		public override void OnActionExecuted(HttpActionExecutedContext context)
		{
			//注意：当Action发生未处理异常会导致Response属性为空
			if(context.Response != null && context.Response.IsSuccessStatusCode)
			{
				//获取当前操作是否支持数据分页加载
				var paging = this.GetPaging(context.ActionContext.ActionArguments);

				if(paging != null)
				{
					//注意：由于数据可能未被加载，因此分页结果尚未计算；故此需要显式加载数据以驱动分页计算
					context.Response.Content.LoadIntoBufferAsync();

					if(paging.PageCount > 0)
					{
						context.Response.Content.Headers.Add("x-paging-size", paging.PageSize.ToString());
						context.Response.Content.Headers.Add("x-paging-index", paging.PageIndex.ToString());
						context.Response.Content.Headers.Add("x-paging-count", paging.PageCount.ToString());
						context.Response.Content.Headers.Add("x-result-totalCount", paging.TotalCount.ToString());
					}
				}
			}

{'fraction_non_alphanumeric': 0.0974745237040319, 'fraction_numerical': 0.007975188303057155, 'mean_word_length': 3.9889502762430937, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3722555', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1447, 'n_words': 416}