c# - ASP.NET Web APi - Passing an object as parameter -
makeuser method in user controller creating username , password.
[httpget] public string makeuser(userparameters p) { const string chars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; string pass = ""; random r = new random(); (int = 0; < p.number; i++) { pass += chars[r.next(0, 62)]; } string firsttwoo = p.name.substring(0, 2); string firstthree = p.surname.substring(0, 3); return "your username is: " + firsttwoo + firstthree + "\nyour password is: " + pass; }
userparameter class sending parameters object.
public class userparameters { public int number { get; set; } public string name { get; set; } public string surname { get; set; } }
runasync method in console client. can pass object method? if yes mistake here? thank you!
static async task runasync() { using (var client = new httpclient()) { var p = new userparameters(); console.write("your username: "); p.name = console.readline(); console.write("your surname: "); p.surname = console.readline(); console.write("please type number between 5 , 10: "); p.number = int.parse(console.readline()); client.baseaddress = new uri("http://localhost:4688/"); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); //http httpresponsemessage response = await client.getasync("api/user?p=" + p); if (response.issuccessstatuscode) { var result = await response.content.readasasync<userparameters>(); console.writeline("\n*****************************\n\n" + result); } } }
your variable p cannot passed query string parameter how have it. populate url , query strings way prefer, have write out rest of query string , access object's properties while building string up.
string querystring = "api/user?name="+p.name+"&surname="+p.surname+"&number="+p.number; httpresponsemessage response = await client.getasync(querystring);
the makeuser() method need similar below:
[httpget] public string makeuser(string name, string surname, int number) { }
i not seeing calling makeuser() method. perhaps in query string parameter need make 'api/makeuser?'.
Comments
Post a Comment