Detailed description of ASP. net ajax Client

Source: Internet
Author: User

In ASP. net ajax, JSON is used to transmit object information between the client and the server. Therefore, the ASP. net ajax client and server provide serialization and deserialization capabilities. Understanding how to use the content can be said to be necessary to use and expand ASP. net ajax. Let's take a look at the serialization and deserialization capabilities in ASP. net ajax.

I. Client serialization and deserialization capabilities:

In ASP. net ajax, The serialize static method of the Sys. Serialization. JavaScriptSerializer class is used to provide Serialization capabilities for the client. This method serializes a client object into a JSON string, which is easy to use. As follows:

 
 
  1. var jsonStr = Sys.Serialization.JavaScriptSerializer.serialize(obj); 

There is not much to say, and it may be a "characteristic" that it serializes the Date object on the client. What will happen if we call the following code?

 
 
  1. var jsonStr = Sys.Serialization.JavaScriptSerializer.serialize(new Date());  


The result is similar to "@ 1162814090119 @". Note that there are double quotation marks on both sides. This is a special expression of ASP. net ajax for Date objects. If developers need to "splice" strings themselves, pay attention to this.

The deserialize static method of the Sys. Serialization. JavaScriptSerializer class is used to bring deserialization capabilities to the ASP. net ajax client. As follows:

 
 
  1. var obj = Sys.Serialization.JavaScriptSerializer.deserialize(jsonStr); 


In fact, it simply calls the JavaScript built-in eval method. Of course, since the Date object has a special representation method during serialization, this will also be taken into account during deserialization: Sys. serialization. the deserialize static method of the JavaScriptSerializer class sets "@... @ "" to "new Date (...) "format, which is a standard JSON string.


Ii. JavaScriptTypeResolver and JavaScriptConverter:

The serialization and deserialization of the client are very simple. I put it here to show that it is more like making the content more complete. Server-side serialization and deserialization are not so easy. They involve a large number of string operations and custom capabilities. This is what I want to emphasize in this article.

The Serialization and deserialization capabilities provided by ASP. net ajax are completed by classes in the Microsoft. Web. Script. Serialization namespace. Fortunately, most of them are internal classes. Only several methods of the JavaScriptSerializer class can be used for developers. ASP. net ajax has brought us ample serialization and deserialization capabilities. We only need to master it and know how they work, which is generally enough.

However, to understand the serialization and deserialization capabilities, you must first understand the other two classes: JavaScriptTypeResolver and JavaScriptConverter.

1. JavaScriptTypeResolver

JavaScriptTypeResolver is an abstract class. Although it is the first time in multiple Release of Atlas, it is not a new thing. The function is equivalent to the IJavaScriptSerializeContext interface in Atlas CTP. It can even be said that the class name and method name are changed from an interface to an abstract class, which is confusing, because the current abstract class does not have any implementation ). This class is used to associate a string with a specific class to make the string an identifier for that specific class ". This abstract class has two methods:

1. String ResolveTypeId (Type): Obtain the ID String of the Type object.
2. Type ResolveType (String): obtains a Type object from the String identifier.

We can see that these two methods are a pair of opposite operations. They will be applied to deserialization respectively. If you are not very familiar with the role of this class, you can take a look at a simple implementation of this abstract class in ASP. net ajax. That is the Microsoft. Web. Script. Serialization. SimpleTypeResolver class. The Code is as follows:

 
 
  1. public sealed class SimpleTypeResolver : JavaScriptTypeResolver  
  2. {  
  3. public override Type ResolveType(string id)  
  4. {  
  5. return Type.GetType(id);  
  6. }  
  7. public override string ResolveTypeId(Type type)  
  8. {  
  9. if (type == null)  
  10. {  
  11. throw new ArgumentNullException("type");  
  12. }  
  13. return type.AssemblyQualifiedName;  
  14. }  


The above introduces the ASP. net ajax Client

  1. ASP. net mvc Web Application Project
  2. IIS6 ASP. net isapi request processing process
  3. Seven user management controls for ASP. NET controls
  4. RSA encryption for ASP. Net
  5. How to obtain database strings using ASP. NET

Related Article

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.