Anyone familiar with Ajax in the tutorial www.bkjia.com should be familiar with the open-source framework DWR, which makes Ajax Program Development simple, but currently it seems that only Java is supported. Let's talk about it in Asp today. how to simulate the DWR function in the. Net project:
Create an interface (called IWAjaxEngine) to define the method exposed to Ajax. I designed a method that returns the string type, and a method that does not return a value, the parameters of both methods are of the Dictionary type. In this way, Ajax can call the background business logic class through this interface type. If the business logic class implements this interface, it can be initialized through class name reflection, the reflection section is described in detail below.
Create an XML file in the App_Data directory as the configuration file. The reason for storing the file in this directory should be known, which can be used for protection. This configuration file specifically configures the information of the business logic class (Class Name, parameter list, parameter type, etc.) that implements the IWAjaxEngine interface ), then create a class (WAjaxConfig) that encapsulates the configuration file to set the instance of this class to static, so that you do not need to read the configuration file every time.
Next we will talk about the key part, that is, how Ajax directly calls the background business logic class. My idea is to create a Web form, that is. aspx and. aspx. cs file, in the page_load event, according to the parameters passed by Ajax (including the ID that can locate the class information in the configuration file) find the name of the class to be called in WAjaxConfig (an exception is thrown if no class is found ), then, create an instance of this class through class name reflection (of course, The IWAjaxEngine interface is used as the instance type). With the instance, we can call the two methods defined by the interface, either a string is returned or nothing is returned.
The whole idea of implementation is like this. It is different from DWR, but it has already met my needs. Of course, it is a bit simple here. It is not very easy to do it. There are many places that need to be refined. In addition, I also made some auxiliary tool classes to generate XML or JSON strings. The front-end also implemented engine similar to the DWR engine script. js and toolset script util. js (actually the util of DWR. when JavaScript was moved over, who made the functions powerful? But some code was also added, because FireFox does not support some js Code that does not comply with the standard DOM, some methods do not support xml dom NodeList. Thank you for the DWR author-_-). In this way, we can program on the front end like using DWR.