Transmission and parsing of JSON data under jquery in JSON value passing Mode Under Asp.net webapi (Asp.net MVC and Asp.net Api have different background JSON receiving methods) ASP. NET webapi RC

Source: Internet
Author: User

 For more information, seeArticleJSON data transmission and parsing in jquery (Asp.net MVC and Asp.net Api have different background JSON receiving methods)

When using webapi earlier, you wanted to post JSON data to the background using Ajax in jquery and receive and process the returned JSON data. However, the backend cannot be shown in the following way in the MVC:

 
Public ilist <site> sitelist (INT startid, int itemcount)

Receives JSON data from the front-end. Then, try to define a class in the background that corresponds to the JSON data type passed by the foreground to find that the class can receive the foreground JSON data. The reason remains unknown.

Later, I found that the following article has the same question.

ASP. NET webapi RC does not support the most common JSON parameter passing.

The article did not point out the specific cause. The comments below provide some inspiration. The JSON transmission method in webapi is different from the previous MVC method. The introduction is as follows:

Question about RC webapi parameter binding

Another friend gave a link pointing out the reasons for different webapi value transfer methods:

 

Reference

Before sending a simple type, consider wrapping the value in a complex type. This gives you the benefits of Model Validation on the server side, and makes it easier to extend your model if needed.

Before passing simple type data, consider whether to wrap the value in a complex type. This will allow you to get model verification on the server side and expand your model as needed.

How to assign values to complex types and simple types:

Reference

By default, web API tries to get simple types from the request URI. The frombody attribute tells web API to read the value from the request body.

Reference

Web API reads the response body at most once, so only one parameter of an action can come from the request body. if you need to get multiple values from the request body, define a complex type.

By default, webapi tries to obtain simple data types from the request URL. You can also add the frombody attribute to notify webapi to read the data you need from the request body.

Webapi only reads the response body once, so only one parameter can be passed from the request body. If you need to obtain the complex type value from the request body, you can define a complex type corresponding to the transmitted JSON data in the background to receive it.

The details are as follows:

Http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1

RThe C-version webapi parameter binder can be divided into two types:

1. Model binding2. formattersModel binding only takes values from the URL, which is different from the MVC model binding. formatters takes values from the request body, in addition, the whole body is parsed as a (not multiple) object as a parameter, and the background needs to define a corresponding class for the complex types passed by the foreground. The model binding system in MVC queries and matches the data in the body and querystring at the same time.

 

RThe C-version webapi parameter binder can be divided into two types:

1. Model binding

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.