myhttphandler.cs

来自「该项目中对 SQLHelper 类进行了简单封装」· CS 代码 · 共 120 行

CS
120
字号
#undef DEBUG	// 用于调试

/* 
 * MyHttpHandler.cs @Microsoft Visual Studio 2008 <.NET Framework 3.5>
 * AfritXia
 * 2006-09-18
 * 
 * Copyright(c) http://www.AfritXia.NET/
 * 
 */

using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Web;

namespace NET.AfritXia.Components.Web
{
	/// <summary>
	/// MyHttpHandler 资源文件请求处理
	/// </summary>
	public class MyHttpHandler : IHttpHandler
	{
		#region IHttpHandler 成员
		public bool IsReusable
		{
			get
			{
				return true;
			}
		}

		public void ProcessRequest(HttpContext context)
		{
			string propName = context.Request["ResID"];

			if (propName == null)
				return;

			// 获取资源类型
			Type type = typeof(MyResources);

			// 获取属性信息
			PropertyInfo propInfo = type.GetProperty(propName, BindingFlags.IgnoreCase | BindingFlags.Static | BindingFlags.NonPublic);

			if (propInfo == null)
				return;

			// 获取资源文件对象
			MyResources myRes = new MyResources();

			// 获取资源值
			object value = propInfo.GetValue(myRes, null);

			if (value == null)
				return;

			switch (value.GetType().ToString())
			{
#if DEBUG
				case "System.Drawing.Bitmap":
					// 设置输出流的 MIME 类型
					context.Response.ContentType = "image/gif";
					// 获取位图图像
					Bitmap bitmap = (System.Drawing.Bitmap)value;
					// 将位图图像保存到页面输出流
					bitmap.Save(context.Response.OutputStream, bitmap.RawFormat);

					break;

				case "System.String":
					// 设置输出流的 MIME 类型
					context.Response.ContentType = "text/css";
					// 设置字符集
					context.Response.ContentEncoding = Encoding.UTF8;

					switch(propName)
					{
						case "CssWebTextPane":
							// 输出 CSS 样式表
							context.Response.Write(Bincess_Resource.CssWebTextPane);
							break;

						case "ScriptWebTextPane":
							// 输出 JavaScript 脚步文件
							context.Response.Write(Bincess_Resource.ScriptWebTextPane);
							break;

						default:
							break;
					}

					break;
#else
				case "System.Drawing.Bitmap":
					// 获取位图图像
					Bitmap bitmap = (System.Drawing.Bitmap)value;
					// 将位图图像保存到页面输出流
					bitmap.Save(context.Response.OutputStream, bitmap.RawFormat);

					break;

				case "System.String":
					// 输出文本值
					context.Response.Write(value);

					break;
#endif
				default:
					break;

			}
		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?