Web Api Create method with multiple parameters
Also, how to overload a method, say GET or PUT?
Well, lets start that with simple settings, you can't. Too many resources with partial examples makes you sometimes believe that it should be that simple (and maybe it should?), while its not.
It's also not that complicated...
Part 1 - understanding default design
When you create a new Web Api project (in vs 2017 Web Application -> empty, mark web api) you get basically nothing but the "WebApiConfig.cs" withing the "interesting" line is
routeTemplate: "api/{controller}/{id}",
And that means that whatever controller you create by default can only have it's 1st parameter, as in the url part, as "id". You cant create a method sending something like "string name".
Next step will be creating a controller class (with the (v2.1) in the end) and you will always get the suggestion of "ValuesController1". You cannot have the end of the controller name other than "Controller", cant add "1", cant omit "Controller".
Clicking OK creates a template for CRUD operations, which currently, is more or less the limit. You can change the return types, the value types for the "value" varialbe, or even omit it (though some frameworks like angular won't let you send empty PUT, not that it's logical)
Trying to overload methods, or sending multiple parameters, will currenlty fail. Also sending "just a string" instead of a JSON object can deceive.
Part 2 - sending multiple variables
*Note - all my JavaScript examples are with angular, but there is really not much difference with "pure"/"vanilla" JS
To send 1 simple type like
Your JS
While any other post you could just send your JSON object.
The one easy way to send multiple parameters, like
Is to just user Query-String instead of Body.
BUT there are a few downsides, the Major one is that Security-wise a Uri is NOT secured, and will not be encrypted via https.
Also design-wise its bad, and, for angular for example, you cant send a body-less PUT. you can solve that by adding one of the parameters as "[FromBody]", but that's just for the POC.
Next, the "GOOD" and suggested way will be to create an object to each request with all it's needs, or to send a generic "JObject".
Finally, sometimes you just want a simple pile of variables, and you can do it with
And your JS
Part 3 - overloading methods
Just overloading methods wont do it and the same method will hit again every-time, even if no match.
You must add an "Action" with the many kinds of attributes decorators C# has to offer you, some are mentioned in this stackoveflow answers.
I preferred the "Route" , while its was a bit tricky to make it work with default settings
In my case, i wanted to update different types of users for their specific fields, so I overloaded PUT
[Route("api/Users/{id}/Realtor")]
There are many more ways to build complex routes, but IMHO this one is the easiest and fastest to implement when only want some basic overloading.
But the main thing to remember is that an overloaded method MUST have a private route.
Well, lets start that with simple settings, you can't. Too many resources with partial examples makes you sometimes believe that it should be that simple (and maybe it should?), while its not.
It's also not that complicated...
Part 1 - understanding default design
When you create a new Web Api project (in vs 2017 Web Application -> empty, mark web api) you get basically nothing but the "WebApiConfig.cs" withing the "interesting" line is
routeTemplate: "api/{controller}/{id}",
And that means that whatever controller you create by default can only have it's 1st parameter, as in the url part, as "id". You cant create a method sending something like "string name".
Next step will be creating a controller class (with the (v2.1) in the end) and you will always get the suggestion of "ValuesController1". You cannot have the end of the controller name other than "Controller", cant add "1", cant omit "Controller".
Clicking OK creates a template for CRUD operations, which currently, is more or less the limit. You can change the return types, the value types for the "value" varialbe, or even omit it (though some frameworks like angular won't let you send empty PUT, not that it's logical)
Trying to overload methods, or sending multiple parameters, will currenlty fail. Also sending "just a string" instead of a JSON object can deceive.
Part 2 - sending multiple variables
*Note - all my JavaScript examples are with angular, but there is really not much difference with "pure"/"vanilla" JS
To send 1 simple type like
public void Post([FromBody]string value)
Your JS
this.http.post("api/controller/id", "=hello!",
{ headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
})
}).subscribe(/* ... */)
While any other post you could just send your JSON object.
The one easy way to send multiple parameters, like
public bool Put(int id, int myParam1, int myParam2, int myParam3, int myParam4)
Is to just user Query-String instead of Body.
BUT there are a few downsides, the Major one is that Security-wise a Uri is NOT secured, and will not be encrypted via https.
Also design-wise its bad, and, for angular for example, you cant send a body-less PUT. you can solve that by adding one of the parameters as "[FromBody]", but that's just for the POC.
Next, the "GOOD" and suggested way will be to create an object to each request with all it's needs, or to send a generic "JObject".
Finally, sometimes you just want a simple pile of variables, and you can do it with
public bool Put(int id, FormDataCollection form)
And your JS
let _headers = { headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
})
}
let body: HttpParams = new HttpParams();
body = body.append('a', val1);
body = body.append('b', val2);
body = body.append('c', val3);
this.http.put(url, body, _headers).subscribe(/* ... */)
Part 3 - overloading methods
Just overloading methods wont do it and the same method will hit again every-time, even if no match.
You must add an "Action" with the many kinds of attributes decorators C# has to offer you, some are mentioned in this stackoveflow answers.
I preferred the "Route" , while its was a bit tricky to make it work with default settings
[Route("api/Users/{id}/Items")]
In my case, i wanted to update different types of users for their specific fields, so I overloaded PUT
[Route("api/Users/{id}/Realtor")]
public bool Put(int id, ...)
[Route("api/Users/{id}/Investor")]
public bool Put(int id, ...)
There are many more ways to build complex routes, but IMHO this one is the easiest and fastest to implement when only want some basic overloading.
But the main thing to remember is that an overloaded method MUST have a private route.
When it has to do with online gambling, there's that age-old question regarding gambling. For more ideal details about online sports, research.
ReplyDelete