Quellcode durchsuchen

Implemented EnablePageMethods feature

svn path=/trunk/mcs/; revision=81238
Igor Zelmanovich vor 18 Jahren
Ursprung
Commit
bd45e92270

+ 11 - 1
mcs/class/System.Web.Extensions/System.Web.Handlers/ScriptModule.cs

@@ -31,6 +31,7 @@ using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Web.UI;
+using System.Web.Script.Services;
 
 namespace System.Web.Handlers
 {
@@ -62,12 +63,21 @@ namespace System.Web.Handlers
 			// the CompleteRequest method is called, bypassing all pipeline events and executing 
 			// the EndRequest method. This allows MS AJAX to be able to call a method on a page 
 			// instead of having to create a web service to call a method.
+			HttpApplication app = (HttpApplication) sender;
+			HttpContext context = app.Context;
+			HttpRequest request = context.Request;
+			string contentType = request.ContentType;
+			if (context.CurrentHandler is Page && !String.IsNullOrEmpty (contentType) && contentType.StartsWith ("application/json", StringComparison.OrdinalIgnoreCase)) {
+				RestHandler h = new RestHandler (((Page) context.CurrentHandler).GetType (), request.FilePath);
+				h.ProcessRequest (context);
+				app.CompleteRequest ();
+			}
 		}
 
 		void PreSendRequestHeaders (object sender, EventArgs e) {
 			HttpApplication app = (HttpApplication) sender;
 			HttpContext context = app.Context;
-			if (context.Request.Headers ["X-MicrosoftAjax"] == "Delta=true"){
+			if (context.Request.Headers ["X-MicrosoftAjax"] == "Delta=true") {
 				if (context.Error != null) {
 					context.Response.StatusCode = 200;
 					context.Response.ClearContent ();

+ 18 - 9
mcs/class/System.Web.Extensions/System.Web.Script.Services/LogicalTypeInfo.cs

@@ -173,8 +173,9 @@ return this._invoke({0}.get_path(), '{1}',{2},{{{3}}},succeededCallback,failedCa
 
 		private LogicalTypeInfo (Type t, string filePath) {
 			_type = t;
-
-			MethodInfo [] all_type_methods = _type.GetMethods (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
+			bool isPage = _type.IsSubclassOf (typeof (System.Web.UI.Page));
+			BindingFlags bindingAttr = isPage ? (BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public) : (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
+			MethodInfo [] all_type_methods = _type.GetMethods (bindingAttr);
 			List<LogicalMethodInfo> logicalMethods = new List<LogicalMethodInfo> (all_type_methods.Length);
 			foreach (MethodInfo mi in all_type_methods) {
 				if (mi.IsPublic && 
@@ -201,21 +202,29 @@ return this._invoke({0}.get_path(), '{1}',{2},{{{3}}},succeededCallback,failedCa
 			for (int i = 0; i < logicalMethods.Count; i++)
 				_methodMap.Add (logicalMethods [i].MethodName, logicalMethods [i]);
 
-			string ns = t.Namespace;
-			string service = t.FullName;
+			string ns = isPage ? String.Empty : t.Namespace;
+			string service = isPage ? "PageMethods" : t.FullName;
 			
 			StringBuilder proxy = new StringBuilder ();
-			proxy.AppendFormat (
+			if (String.IsNullOrEmpty (ns))
+				proxy.AppendFormat (
+@"var {0}",
+	service);
+			else
+				proxy.AppendFormat (
 @"Type.registerNamespace('{0}');
-{1}=function() {{
-{1}.initializeBase(this);
+{1}",
+	ns, service);
+			proxy.AppendFormat (
+@"=function() {{
+{0}.initializeBase(this);
 this._timeout = 0;
 this._userContext = null;
 this._succeeded = null;
 this._failed = null;
 }}
-{1}.prototype={{",
-			ns, service);
+{0}.prototype={{",
+			service);
 
 			for (int i = 0; i < logicalMethods.Count; i++) {
 				if (i > 0)

+ 9 - 2
mcs/class/System.Web.Extensions/System.Web.UI/ScriptManager.cs

@@ -42,6 +42,7 @@ using System.IO;
 using System.Globalization;
 using System.Threading;
 using System.Web.Script.Serialization;
+using System.Web.Script.Services;
 
 namespace System.Web.UI
 {
@@ -108,6 +109,7 @@ namespace System.Web.UI
 		bool _init;
 		string _panelToRefreshID;
 		Dictionary<Control, DataItemEntry> _dataItems;
+		bool _enablePageMethods;
 
 		[DefaultValue (true)]
 		[Category ("Behavior")]
@@ -166,10 +168,10 @@ namespace System.Web.UI
 		[DefaultValue (false)]
 		public bool EnablePageMethods {
 			get {
-				throw new NotImplementedException ();
+				return _enablePageMethods;
 			}
 			set {
-				throw new NotImplementedException ();
+				_enablePageMethods = value;
 			}
 		}
 
@@ -421,9 +423,14 @@ namespace System.Web.UI
 					string script = String.Format ("var __cultureInfo = '{0}';", _cultureInfoSerializer.Serialize (culture));
 					RegisterClientScriptBlock (this, typeof (ScriptManager), "ScriptGlobalization", script, true);
 				}
+				if (EnablePageMethods) {
+					LogicalTypeInfo logicalTypeInfo = LogicalTypeInfo.GetLogicalTypeInfo (Page.GetType (), Page.Request.FilePath);
+					RegisterStartupScript (this, typeof (ScriptManager), "PageMethods", logicalTypeInfo.Proxy, true);
+				}
 
 				// Register startup script
 				StringBuilder sb = new StringBuilder ();
+				sb.AppendLine ();
 				sb.AppendLine ("Sys.Application.initialize();");
 				RegisterStartupScript (this, typeof (ExtenderControl), "Sys.Application.initialize();", sb.ToString (), true);
 			}