Preface
I'm getting NET back! The road to Java is really a tragic fate, and good things are hard to deal with. However, maybe I have reached the point where there is no move, and I can gain the strengths of all schools and integrate them.
Today I have some new understanding of WebApi.
In the Get request method, the parameters will be attached to the Url, called QueryString, and passed to the server; while the POST method will place the parameters in the message body. QueryString is simple and convenient, but only suitable for situations where there are fewer parameters; but sometimes, it is necessary to pass more and more complex parameters, such as combination condition query.
Combining conditional query will have many conditions, and it will usually be encapsulated with an entity class and passed to the server. It is definitely OK to use post method, you can be familiar with it easily. The problem is that the RESTful principle is to abstract everything into resources, and different ways of requesting resources represent different operations on resources. Logically, doesn’t post mean insertion? Why do you need to use post for querying?
In GET mode, can parameters be submitted to the server? But after searching, ajax seems to be OK; but on the server side, using WebClient, no ready-made examples were found, and I don’t know what to do. I had to use the most primitive method to convert the entity class into a QueryString and attach it to the address. There are two problems:
1. How to convert entity classes into a key-value pair format like QueryString?
2. How to extract it on the server side?
1. How to convert entity classes into a key-value pair format like QueryString?
It may not be professional enough to call it NameValueCollection?
Unfortunately, no ready-made examples were found.
Finally, use the reflection to assemble the QueryString
2. How to extract the parameters in QueryString on the server and automatically turn them into an entity class?
The parameters are marked with [FromUri] characteristics
On the code.
Entity Class:
namespace { 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(); } }
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 do sth; } }
Client:
[TestMethod] public void TestTankApi() { string url = "http://localhost//api/tank/matters/public/"; url += getQueryString(new Request() { PageIndex = 1, PageSize = 100 }); string re; using (WebClient webClient = new WebClient()) { = ("utf-8"); re = (url); } (null, re); (re); } static string getQueryString(Request req) { StringBuilder query = new StringBuilder("?"); PropertyInfo[] propertys = ().GetProperties(); foreach (PropertyInfo pi in propertys) { if () { ($@"{}={(req)}&"); } } return (); }
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.