For ASP. net web API was born, coupled with its open source (Open Source), so a little infatuated with it, and wrote httpclient + ASP.. NET web API. At that time, ASP. NET web API was still in beta stage. As the saying goes, the 18th change of female university naturally gave rise to the embarrassment of ASP. NET web api rc...
After the debut of ASP. NET web api rc, some people have repeatedly reported the examples in the previous blog.CodeIt cannot run properly in ASP. NET web api rc. In this case, we use ASP.. NET web API project upgrade to ASP. net web api rc also encountered the same problem. After the JSON format data is post to the server through httpclien, the corresponding action in the server controller cannot receive data, and the error message is: "the parameters dictionary contains a null entry for parameter 'startid' of non-nullable type 'System. int32 'For method... ".
I thought it was a problem with httpclient. The previous Code serialized the data to be passed into a JSON string through JSON. net. The Code is as follows:
VaR Requestjson = jsonconvert. serializeobject ( New {Startid = 1 , Itemcount =3 }); Httpcontent = New Stringcontent (requestjson); httpcontent. headers. contenttype = New Mediatypeheadervalue ( " Application/JSON " ); VaR Httpclient = New Httpclient (); VaR Responsejson = httpclient. postasync (" HTTP: /localhost: 9000/API/demo/sitelist " , Httpcontent). Result. content. readasstringasync (). result;
One of the improvements to ASP. NET web api rc is the built-in support for JSON. net, so the data in post JSON format is simpler. The above code can be changed:
VaRResponsejson =NewHttpclient (). postasjsonasync ("Http://test.cnblogs.cc/api/demo/sitelist",New{Startid =1, Itemcount =3}). Result. content. readasstringasync (). result;
However, after the httpclient code is changed, the problem persists. You can use Fiddler to view the post data. The original authentic JSON data seems to be the problem on the server side.
At this time, someone reported that JSON data is submitted through jquery's $. Ajax, and the server cannot receive it. You don't have to think about it. The problem must be on the server. See examplesProgramServer code:
Public ClassDemocontroller: apicontroller {PublicIlist <site> sitelist (IntStartid,IntItemcount ){...ReturnResult ;}}
In the RC version, ASP. NET Web APIs change the processing method of post data traditionally sent to action. First of all, it is certain that ASP. NET web api rc does not support JSON parameters. Is it necessary to define a class for this purpose (for example, sitelistquery) and use the current parameters as the attributes of the class. The Code is as follows:
Public class democontroller: apicontroller { Public ilist
sitelist (sitelistquery query ){...
return
result ;}}
Public
class
sitelistquery {
Public
int startid {
Get ;
set
;}
Public
int itemcount {
Get ;
set
;}
In this case, you need to define many such parameter classes, and define a class even if there is only one parameter. If so, I would rather not use ASP. NET Web APIs.
At that time, I did not try it further. I thought that since common JSON parameter passing methods are not supported, I don't need to use ASP. NET web API RC for the moment.
Today, a colleague confirmed that a special parameter class must be defined to support JSON parameter passing.
When my colleagues told me, the feeling of love, hate, and friendship came out. How can we design it like this? Even if this design has its own principles, we cannot give up on compatibility with the common usage. Good compatibility is the foundation of Microsoft's startup. How can I be ignored here? ViewSource codeAnd there is no complete test code. ASP. NET may have been the inactive plug-in of Microsoft, But Now Liu chengmeng, why not develop into a forest by taking advantage of the trend? How can I create a future without leaving my desktop? Whether ASP. NET web stack is open-source because it does not matter or because it really wants to passCommunityIs it better to develop? In such a critical period, is Microsoft catching up with others' advantages with its own weaknesses, or is Microsoft surpassing others with its own advantages?
It is not ASP. NET web API, but a more elegant technology to better solve practical problems;
Unexpectedly, this is not a poor design, and it is not really a developer's perspective.
(Note: In one breath, it is fast. It is also a way to write a blog. I hope you will forgive me for writing anything wrong)
Download the sample code: cnblogswebapircdemo.rar