Define JavaScriptConverter in ASP. NET

Source: Internet
Author: User

Sometimes you need to use complex types in Web services. Their features are often referenced cyclically. If such objects are handed over to ASP. the default serialization method in net ajax will throw an exception, and the "able" problem that people often encounter is caused by this. ASP. net ajax naturally provides a solution. Here, the "official" solution is JavaScriptConverter, which allows developers to customize specific types of serialization capabilities.

In fact, you need to customize the JavaScriptConverter type to not only include the "loop reference" type. In fact, the objective of JavaScriptConverter is "ASP.. net ajax cannot be operated, or the result is not the type that the developer expects. The "operation" here includes "serialization" and "deserialization. For another example, if a constructor type does not have any parameters, you also need to define JavaScriptConverter. Otherwise, ASP. net ajax cannot deserialize it.

Let's take a simple example to see how to develop and use JavaScriptConverter.

1. define the type of cyclic reference

First, we define a Boy class and Girl class for use:

 
 
  1. public class Boy  
  2. {  
  3. public string Name;  
  4. public Girl GirlFriend;  
  5. }  
  6. public class Girl  
  7. {  
  8. public string Name;  
  9. public Boy BoyFriend;  

Obviously, if I "match them as a pair", an exception will be thrown during serialization output. At this point, we have to define a JavaScriptConverter, which cannot be broken up.


2. Define JavaScriptConverter and serialization capability

Let's start to define JavaScriptConverter. Let's call it BoyConverter. First, we need to tell ASP. NET what types can be supported by Converter:

 
 
  1. public class BoyConverter : JavaScriptConverter  
  2. {  
  3. public override IEnumerable<Type> SupportedTypes  
  4. {  
  5. get  
  6. {  
  7. yield return typeof(Boy);  
  8. }  
  9. }  
  10. ……  

If you want to implement a good Serialize method, you need to deal with the two situations: "circular reference" and "No circular reference. Fortunately, this is easier for the Serialize method:

 
 
  1. Public override IDictionary<String, Object>Serialize (object obj,
    JavaScriptSerializer serializer)
  2. {
  3. IDictionary<String, Object> Result=NewDictionary<String, Object>();
  4. BoyBoy= (Boy) obj;
  5. Result ["Name"] = boy. Name;
  6. // If GirlFriend references
  7. If (boy. GirlFriend! = Null)
  8. {
  9. // Remove the circular reference
  10. Boy. GirlFriend. BoyFriend=Null;
  11. Result ["GirlFriend"] = boy. GirlFriend;
  12. // Re-establish association on the client
  13. Result ["_ getRealObject"] =
  14. "Function (o ){OO. GirlFriend. BoyFriend= O; return o ;}";
  15. }
  16. Return result;
  17. }

Here, we manually convert the Boy object to an IDictionary <string, objct> to avoid circular references. In addition, to directly obtain a referenced "Boy" and "Girl" objects on the client, I used the extension I mentioned in the previous article here, for details, see in-depth Atlas series: comprehensive example 1)-directly obtain the specific client type when calling the server method.


3. Example of custom serialization

Let's take a look at the example. First, we need to define a Web Service method:

 
 
  1. [WebService(Namespace = "http://tempuri.org/")]  
  2. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  3. [Microsoft.Web.Script.Services.ScriptService]  
  4. public class BoyGirlService  : System.Web.Services.WebService {  
  5. [WebMethod]  
  6. public Boy GetBoy(string boyName, string girlName)  
  7. {  
  8. Boy boy = new Boy();  
  9. boy.Name = boyName;  
  10. if (!String.IsNullOrEmpty(girlName))  
  11. {  
  12. Girl girl = new Girl();  
  13. girl.Name = girlName;  
  14. girl.BoyFriend = boy;  
  15. boy.GirlFriend = girl;  
  16. }  
  17. return boy;  
  18. }  

The preceding section defines JavaScriptConverter in ASP. NET.

  1. WebRequestExecutor in ASP. NET
  2. IIS6 ASP. net isapi request processing process
  3. Backup in ASP. NET
  4. Introduction to ASP. NET ISAPI
  5. Iis isapi extension of ASP. NET

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.