ASP.NET Client to Server communication

为君一笑 提交于 2020-01-13 11:02:28

问题


Can you help me make sense of all the different ways to communicate from browser to client in ASP.NET? I have made this a community wiki so feel free to edit my post to improve it. Specifically, I'm trying to understand in which scenario to use each one by listing how each works.

I'm a little fuzzy on UpdatePanel vs CallBack (with ViewState): I know UpdatePanel always returns HTML while CallBack can return JSON. Any other major differences?

...and CallBack (without ViewState) vs WebMethod. CallBack goes through most of the Page lifecycle, WebMethod doesn't. Any other major differences?

IHttpHandler

  • Custom handler for anything (page, image, etc.)
    • Only does what you tell it to do (light server processing, light traffic)
    • Page is an implementation of IHttpHandler
    • If you don't need what Page provides, create a custom IHttpHandler
    • If you are using Page but overriding Render() and not generating HTML, you probably can do it with a custom IHttpHandler (e.g. writing binary data such as images)
  • By default can use the .axd or .ashx file extensions -- both are functionally similar
    • .ashx doesn't have any built-in endpoints, so it's preferred by convention

Regular PostBack (System.Web.UI.Page : IHttpHandler)

  • Inherits Page
    • Full PostBack, including ViewState and HTML control values (heavy traffic)
    • Full Page lifecycle (heavy server processing)
  • No JavaScript required
  • Webpage flickers/scrolls since everything is reloaded in browser
  • Returns full page HTML (heavy traffic)

UpdatePanel (System.Web.UI.Control)

  • Control inside Page
    • Full PostBack, including ViewState and HTML control values (heavy traffic)
    • Full Page lifecycle (heavy server processing)
    • Controls outside the UpdatePanel do Render(NullTextWriter)
  • Must use ScriptManager
    • If no client-side JavaScript, it can fall back to regular PostBack with no JavaScript (?)
  • No flicker/scroll since it's an async call, unless it falls back to regular postback.
  • Can be used with master pages and user controls
  • Has built-in support for progress bar
  • Returns HTML for controls inside UpdatePanel (medium traffic)

Client CallBack (Page, System.Web.UI.ICallbackEventHandler)

  • Inherits Page
    • Most of Page lifecycle - no render but control tree has to be created (heavy server processing)
  • Takes only data you specify (light traffic) and optionally ViewState (?) (medium traffic)
  • Client must support JavaScript and use ScriptManager
  • No flicker/scroll since it's an async call
  • Can be used with master pages and user controls
  • Returns only data you specify in format you specify (e.g. JSON, XML...) (?) (light traffic)
  • During a callback, the page must add the associated control at the same location in the control tree, otherwise an Exception occurs.

WebMethod (System.Web.Service.WebService)

  • Considered legacy technology. Should use WCF service instead.
  • Class implements System.Web.Service.WebService
    • HttpContext available through this.Context
  • Takes only data you specify (light traffic)
  • Server only runs the called method (light server processing)
  • Client must support JavaScript
  • No flicker/scroll since it's an async call
  • Can be used with master pages and user controls
  • Returns only data you specify, typically JSON (light traffic)
    • Can create instance of server control to render HTML and sent back as string, but events, paging in GridView, etc. won't work

PageMethods

  • Essentially a WebMethod contained in the Page class, so most of WebMethod's bullet's apply
    • As with WebMethod, legacy technology and WCF Service should be used.
    • Method must be public static, therefore no Page instance accessible
    • HttpContext available through HttpContext.Current
  • Accessed directly by URL Page.aspx/MethodName (e.g. with XMLHttpRequest directly or with library such as jQuery)
    • Setting ScriptManager property EnablePageMethods="True" generates a JavaScript proxy for each WebMethod
  • Cannot be used directly in user controls
  • with master pages and user controls

WCF Service

  • Supersedes WebMethod and PageMethods
  • Interface has ServiceContract attribute
  • Method has OperationContract attribute
  • Same benefits as WebMethod, plus more flexible

Any others?


回答1:


WebMethod is used with ASMX web services, which Microsoft now considers to be "legacy technology". WCF services should be used instead. They support both SOAP over HTTP/HTTPS and REST-based or JSON services, so are much more flexible.

For this reason, I also recommend against PageMethods, even if they seem to be convenient.




回答2:


For ICallbackEventHandler

  1. ViewState is always sent to the server in the ajax request, so it can be read and control state can be re-instantiated, but the ViewState is not updated on the server, or sent back to the browser in the ajax response.
  2. Form values are posted back to the server in the request.


来源:https://stackoverflow.com/questions/3017394/asp-net-client-to-server-communication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!