I got dunet back! The Java road has been a bumpy one. Perhaps, however, I have reached the point where I can take all the best from all the best.

I learned something new about WebApi today.

In other words, Get requests, the parameters are attached to the Url, called QueryString, and passed to the server; POST puts parameters in the message body.

Using QueryString, simple, convenient, but only suitable for fewer parameters; Sometimes, however, you need to pass more complex parameters, such as combinatorial conditional queries. That said, the conditions can be many, often encapsulated in an entity class passed to the server. Post is definitely ok. It’s a breeze. The problem is that the RESTful principle abstracts everything into a resource, and different ways of requesting a resource represent different operations on that resource. Doesn’t post logically stand for insert? Why do queries also use POST?

Is it possible to submit entity parameters to the server in GET mode? But looking around, Ajax seems to work; However, on the server side, there is no ready-made example to be found using WebClient and I don’t know what to do. We had to use the primitive method to convert the entity class to a QueryString and attach it to the address. There are two questions:

How does an entity class convert to QueryString? 2. How to extract on the server side?

How can an entity class be converted to QueryString? It’s probably unprofessional to call a key-value pair, NameValueCollection? Unfortunately, there are few examples. Finally, reflection is used to assemble QueryString

How does the server extract QueryString parameters and automatically turn them into an entity class? The parameter is preceded by the [FromUri] feature

In the code.

Entity class:

namespace BaseLT.Core.Contract
{
    public class Request
    {
        public Request();

        public int Top { set; }
        public int PageSize { get; set; }
        public int PageIndex { get; set; }
        public string OrderBy { get; set; }
        public int SortState { get; set; }

        public bool CompareObject<T> (T obj1, T obj2);
        public void ExtjsInit(); }}Copy the code

WebApi server side:

public class TankController : ApiController{[HttpGet]
	[Route("api/tank/matters/public/{id=0}")]
	public IEnumerable<Matter> Get(int id,[FromUri]Request req)
	{
		return dosth; }}Copy the code

Client:

[TestMethod]
public void TestTankApi()
{
	string url = "http://localhost/ybjzuser.api/api/tank/matters/public/";
	url += getQueryString(new Request()
	{
		PageIndex = 1,
		PageSize = 100
	});
	
	string re;
	using (WebClient webClient = new WebClient())
	{
		webClient.Encoding = Encoding.GetEncoding("utf-8");
		re = webClient.DownloadString(url);
	}
	Assert.AreNotEqual(null, re);
	Console.WriteLine(re);
}
static string getQueryString(Request req)
{
	StringBuilder query = new StringBuilder("?");

	PropertyInfo[] propertys = req.GetType().GetProperties();
	foreach (PropertyInfo pi in propertys)
	{
		if (pi.CanRead)
		{
			query.Append($@"{pi.Name}={pi.GetValue(req)}&"); }}return query.ToString();
}
Copy the code

ASP.NET for WebApi submits data by interface, content-type should be application/x-www-form-urlencoded or application/json?