ajaxhandlerfactory.cs

来自「AJAX开发工具包」· CS 代码 · 共 113 行

CS
113
字号
/*
 * MS	06-04-11	added use of IHttpAsyncHandler when configured with AjaxMethod attribute
 * 
 * 
 */
using System;
using System.IO;
using System.Web;
using System.Web.Caching;

namespace AjaxPro
{
	public class AjaxHandlerFactory : IHttpHandlerFactory
	{
		#region IHttpHandlerFactory Members

		public void ReleaseHandler(IHttpHandler handler)
		{
			// TODO:  Add AjaxHandlerFactory.ReleaseHandler implementation
		}

		public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
		{
			// First of all we want to check what a request is running. There are three different
			// requests that are made to this handler:
			//		1) GET core.ashx which will include the common AJAX communication
			//		2) GET typename,assemblyname.ashx which will return the AJAX wrapper JavaScript code
			//		3) POST typename,assemblyname.ashx which will invoke a method.
			// The first two requests will return the JavaScript code or a HTTP 304 (not changed).
			
			string filename = Path.GetFileNameWithoutExtension(context.Request.Path);
			Type t = null;

			switch(requestType)
			{
				case "GET":		// get the JavaScript files

					switch(filename.ToLower())
					{
						case "core":
							return new EmbeddedJavaScriptHandler("core");

						case "prototype":
							return new EmbeddedJavaScriptHandler("prototype");

						case "converter":
							return new ConverterJavaScriptHandler();

						default:

							if(Utility.Settings.UrlNamespaceMappings.Contains(filename))
								t = Type.GetType(Utility.Settings.UrlNamespaceMappings[filename].ToString());

							if(t == null)
								t = Type.GetType(filename);

							return new TypeJavaScriptHandler(t);
					}

				case "POST":	// invoke the method

					if(Utility.Settings != null && Utility.Settings.UrlNamespaceMappings.Contains(filename))
						t = Type.GetType(Utility.Settings.UrlNamespaceMappings[filename].ToString());

					if(t == null)
						t = Type.GetType(filename);
					

					IAjaxProcessor[] p = new IAjaxProcessor[2];
					
					p[0] = new XmlHttpRequestProcessor(context, t);
					p[1] = new IFrameProcessor(context, t);

					for(int i=0; i<p.Length; i++)
					{
						if(p[i].CanHandleRequest)
						{
							AjaxMethodAttribute[] ma = (AjaxMethodAttribute[])p[i].Method.GetCustomAttributes(typeof(AjaxMethodAttribute), true);

							if(ma.Length > 0)
							{
								if (ma[0].RequireSessionState == HttpSessionStateRequirement.Read)
								{
									if(!ma[0].UseAsyncProcessing)
										return new AjaxSyncHttpHandlerSessionReadOnly(p[i]);
									else
										return new AjaxAsyncHttpHandlerSessionReadOnly(p[i]);
								}
								else if (ma[0].RequireSessionState == HttpSessionStateRequirement.ReadWrite)
								{
									if(!ma[0].UseAsyncProcessing)
										return new AjaxSyncHttpHandlerSession(p[i]);
									else
										return new AjaxAsyncHttpHandlerSession(p[i]);
								}
							}

							if(!ma[0].UseAsyncProcessing)
								return new AjaxSyncHttpHandler(p[i]);
							else
								return new AjaxAsyncHttpHandler(p[i]);
						}
					}
					break;
			}
		
			return null;
		}

		#endregion
	}
}

⌨️ 快捷键说明

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