【问题标题】:Why instance of the page is created when PageMethod is called from client?为什么从客户端调用 PageMethod 时会创建页面实例?
【发布时间】:2011-10-19 22:19:54
【问题描述】:

由于 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      
            });

【问题讨论】:

  • 您是在问为什么要为您的页面调用构造函数,或者您需要做什么才能在 PageMethod 中构建页面?
  • @Brain - 我只是想了解为什么在尝试调用静态方法(页面方法)时会调用页面的构造函数
  • Moorthy,这不可能(因为它是静态方法)!仔细看调用堆栈。请显示 webmehod 代码。可能会发起回发吗?
  • @vladimir77 - 请参阅更新后的问题。我已经把代码sn-p。
  • 哇!没错,就是调用了构造函数!观察调用堆栈..

标签: asp.net pagemethods


【解决方案1】:

编辑

确实调用了构造函数!!查看 pagemethod 调用的调用栈:

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] - 仅由 ajax 调用 cos 页面方法(无需创建页面)。

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

【讨论】:

  • 这与我的理解相同。但是由于某种原因,当我进行 ajax 调用时,页面的构造函数被调用,这意味着即使是 ajax 调用也会创建一个实例
  • 感谢您提供堆栈跟踪。所以很明显,构造函数是由 ASP.NET 框架本身调用的。
猜你喜欢
  • 1970-01-01
  • 2018-07-27
  • 2012-09-05
  • 1970-01-01
  • 2012-02-15
  • 2011-11-21
  • 1970-01-01
  • 2015-09-22
  • 2020-08-11
相关资源
最近更新 更多