コード例 #1
0
		public void ProcessRequest(HttpContext context)
		{
			this.context = context;

			// it's possible to the requestor to be able to handle everything himself, overriding all this implemention
			string handleRequest = context.Request["handlerequest"];
			if (!string.IsNullOrEmpty(handleRequest) && handleRequest.ToLower() == "true")
			{
				HandleRequest();
				return;
			}

			var ajaxCall = new AjaxCallSignature(context);

			//context.Response.ContentType = string.Empty;
			if (!string.IsNullOrEmpty(ajaxCall.returnType))
			{
				switch (ajaxCall.returnType)
				{
					case "json":
						context.Response.ContentType = "application/json";
						break;
					case "xml":
						context.Response.ContentType = "application/xml";
						break;
					case "jpg":
					case "jpeg":
					case "image/jpg":
						context.Response.ContentType = "image/jpg";
						break;
					default:
						break;
				}
			}

			// call the requested method
			object result = ajaxCall.Invoke(this, context);	

			// if neither on the arguments or the actual method the content type was set then make sure to use the default content type
			if (string.IsNullOrEmpty(context.Response.ContentType) && !SkipContentTypeEvaluation)
			{
				context.Response.ContentType = DefaultContentType();
			}

			// only skip transformations if the requestor explicitly said so
			if (result == null)
			{
				context.Response.Write(string.Empty);
			}
			else if (!SkipDefaultSerialization)
			{
				switch (context.Response.ContentType.ToLower())
				{
					case "application/json":
						JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
						string json = jsonSerializer.Serialize(result);
						context.Response.Write(json);
						break;
					case "application/xml":
						System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(result.GetType());
						StringBuilder xmlSb = new StringBuilder();
						System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(xmlSb);
						xmlSerializer.Serialize(xmlWriter, result);
						context.Response.Write(xmlSb.ToString());
						break;
					case "text/html":
						context.Response.Write(result);
						break;
					default:
						throw new Exception(string.Format("Unsuported content type [{0}]", context.Response.ContentType));
				}
			}
			else
			{
				context.Response.Write(result);
			}
		}