为什么从客户端调用PageMethod时会创建页面实例?(Why instance of the page is created when PageMethod is called from clie

编程入门 行业动态 更新时间:2024-10-27 18:17:34
为什么从客户端调用PageMethod时会创建页面实例?(Why instance of the page is created when PageMethod is called from client?)

由于ASP.NET中的PageMethods需要是静态方法并标记为Web方法,我的印象是在调用页面方法时不会创建该特定页面的实例。

但是当我尝试在构造函数中设置一个断点时,每次调用pagemethod时都会遇到它。 有人能告诉我构建实例需要什么吗?

谢谢

服务器端

public partial class PageMethod : System.Web.UI.Page { public PageMethod() { } [System.Web.Services.WebMethod()] public static string GetMessage() { return "Page Method Call"; } }

客户端(使用JQuery)

$.ajax({ type: 'POST', url: /PageMethod.aspx/GetMessage, data: null, success: onSuccess, contentType: "application/json; charset=utf-8", dataType: 'JSON', error: onError });

As PageMethods in ASP.NET need to be static methods and marked as web method, I was in a impression that instance of that particular page won't be created when a page method is called.

But when I tried putting a break point in the constructor it was getting hit every time a pagemethod is called. Can somebody let me know what is need for constructing an instance?

Thanks

Server side

public partial class PageMethod : System.Web.UI.Page { public PageMethod() { } [System.Web.Services.WebMethod()] public static string GetMessage() { return "Page Method Call"; } }

Client Side (used JQuery)

$.ajax({ type: 'POST', url: /PageMethod.aspx/GetMessage, data: null, success: onSuccess, contentType: "application/json; charset=utf-8", dataType: 'JSON', error: onError });

最满意答案

编辑

确实是构造函数! 查看pagemethod调用的callstack:

PageMethodTest_WebApplication3.DLL!PageMethodTest_WebApplication3._Default._Default() Line 9 C# App_Web_iielssqo.dll!ASP.default_aspx.default_aspx() + 0x43 bytes C# App_Web_iielssqo.dll!__ASP.FastObjectFactory_app_web_iielssqo.Create_ASP_default_aspx() + 0x43 bytes C# System.Web.dll!System.Web.Compilation.BuildResultCompiledType.CreateInstance() + 0x21 bytes System.Web.dll!System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(System.Web.VirtualPath virtualPath, System.Type requiredBaseType, System.Web.HttpContext context, bool allowCrossApp, bool noAssert) + 0x78 bytes System.Web.dll!System.Web.UI.PageHandlerFactory.GetHandlerHelper(System.Web.HttpContext context, string requestType, System.Web.VirtualPath virtualPath = {/Default.aspx}, string physicalPath) + 0x22 bytes System.Web.dll!System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(System.Web.HttpContext context, string requestType, System.Web.VirtualPath virtualPath, string physicalPath) + 0x29 bytes System.Web.dll!System.Web.HttpApplication.MapHttpHandler(System.Web.HttpContext context, string requestType, System.Web.VirtualPath path, string pathTranslated = "C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2008\\Projects\\PageMethodTest_WebApplication3\\PageMethodTest_WebApplication3\\Default.aspx", bool useAppConfig) + 0xa1 bytes

ASP.NET引擎将请求从pagemethod路由到aspx-handler-factory(PageHandlerFactory)。 据我所知,这是ASP.NET的内在逻辑。 所以它是正确的:)

现在检查asmx-webmethod调用:

PageMethodTest_WebApplication3.DLL!PageMethodTest_WebApplication3.WebService1.HelloWorld() Line 22 C# [Native to Managed Transition] [Managed to Native Transition] System.Web.Extensions.dll!System.Web.Script.Services.WebServiceMethodData.CallMethod(object target, System.Collections.Generic.IDictionary<string,object> parameters) + 0x15c bytes System.Web.Extensions.dll!System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(object target, System.Collections.Generic.IDictionary<string,object> parameters) + 0x1f bytes System.Web.Extensions.dll!System.Web.Script.Services.RestHandler.InvokeMethod(System.Web.HttpContext context = {System.Web.HttpContext}, System.Web.Script.Services.WebServiceMethodData methodData = {System.Web.Script.Services.WebServiceMethodData}, System.Collections.Generic.IDictionary<string,object> rawParams) + 0x61 bytes System.Web.Extensions.dll!System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(System.Web.HttpContext context = {System.Web.HttpContext}, System.Web.Script.Services.WebServiceMethodData methodData) + 0x55 bytes System.Web.Extensions.dll!System.Web.Script.Services.RestHandler.ProcessRequest(System.Web.HttpContext context) + 0xc bytes System.Web.Extensions.dll!System.Web.Script.Services.ScriptHandlerFactory.HandlerWrapper.ProcessRequest(System.Web.HttpContext context) + 0xe bytes System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0xb6 bytes

这里使用另一个httphandler-factory - ScriptHandlerFactory。

PS Moorthy,谢谢! 好问题:)'生活和学习!'


static / [ WebMethod ] - cos页面方法仅由ajax调用(不创建页面)。

如果你想拥有Page实例,你应该使用UpdatePanel或AJAX.NET控件(例如DevExpress , AjaxToolKit )。

EDIT

Indeed constructor is called!! See callstack of pagemethod's call:

PageMethodTest_WebApplication3.DLL!PageMethodTest_WebApplication3._Default._Default() Line 9 C# App_Web_iielssqo.dll!ASP.default_aspx.default_aspx() + 0x43 bytes C# App_Web_iielssqo.dll!__ASP.FastObjectFactory_app_web_iielssqo.Create_ASP_default_aspx() + 0x43 bytes C# System.Web.dll!System.Web.Compilation.BuildResultCompiledType.CreateInstance() + 0x21 bytes System.Web.dll!System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(System.Web.VirtualPath virtualPath, System.Type requiredBaseType, System.Web.HttpContext context, bool allowCrossApp, bool noAssert) + 0x78 bytes System.Web.dll!System.Web.UI.PageHandlerFactory.GetHandlerHelper(System.Web.HttpContext context, string requestType, System.Web.VirtualPath virtualPath = {/Default.aspx}, string physicalPath) + 0x22 bytes System.Web.dll!System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(System.Web.HttpContext context, string requestType, System.Web.VirtualPath virtualPath, string physicalPath) + 0x29 bytes System.Web.dll!System.Web.HttpApplication.MapHttpHandler(System.Web.HttpContext context, string requestType, System.Web.VirtualPath path, string pathTranslated = "C:\\Documents and Settings\\Administrator\\My Documents\\Visual Studio 2008\\Projects\\PageMethodTest_WebApplication3\\PageMethodTest_WebApplication3\\Default.aspx", bool useAppConfig) + 0xa1 bytes

ASP.NET engine routes request from pagemethod to aspx-handler-factory (PageHandlerFactory). As far as I understand it's inner logic of ASP.NET. Therefore it's correct :)

Now check asmx-webmethod call:

PageMethodTest_WebApplication3.DLL!PageMethodTest_WebApplication3.WebService1.HelloWorld() Line 22 C# [Native to Managed Transition] [Managed to Native Transition] System.Web.Extensions.dll!System.Web.Script.Services.WebServiceMethodData.CallMethod(object target, System.Collections.Generic.IDictionary<string,object> parameters) + 0x15c bytes System.Web.Extensions.dll!System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(object target, System.Collections.Generic.IDictionary<string,object> parameters) + 0x1f bytes System.Web.Extensions.dll!System.Web.Script.Services.RestHandler.InvokeMethod(System.Web.HttpContext context = {System.Web.HttpContext}, System.Web.Script.Services.WebServiceMethodData methodData = {System.Web.Script.Services.WebServiceMethodData}, System.Collections.Generic.IDictionary<string,object> rawParams) + 0x61 bytes System.Web.Extensions.dll!System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(System.Web.HttpContext context = {System.Web.HttpContext}, System.Web.Script.Services.WebServiceMethodData methodData) + 0x55 bytes System.Web.Extensions.dll!System.Web.Script.Services.RestHandler.ProcessRequest(System.Web.HttpContext context) + 0xc bytes System.Web.Extensions.dll!System.Web.Script.Services.ScriptHandlerFactory.HandlerWrapper.ProcessRequest(System.Web.HttpContext context) + 0xe bytes System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0xb6 bytes

Here using another httphandler-factory - ScriptHandlerFactory.

PS Moorthy, thanks! Good question :) 'Live and learn!'


static / [WebMethod] - cos page method is called solely by ajax (without page creation).

If you wanna have Page instance you should use UpdatePanel or AJAX.NET Controls (for instance DevExpress, AjaxToolKit).

更多推荐

public,PageMethod,Web,电脑培训,计算机培训,IT培训"/> <meta name="descrip

本文发布于:2023-08-06 08:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446145.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时会   客户端   实例   页面   called

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!