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
public void Post([FromBody]string value)

Your JS
  { 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 idint myParam1int myParam2int myParam3int 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 idFormDataCollection form)

And your JS

let _headers =  { headers: new HttpHeaders({
  'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',

let bodyHttpParams = new HttpParams();
body = body.append('a'val1);
body = body.append('b'val2);
body = body.append('c'val3);

this.http.put(urlbody_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

In my case, i wanted to update different types of users for their specific fields, so I overloaded PUT
public bool Put(int id, ...)

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.


  1. When it has to do with online gambling, there's that age-old question regarding gambling. For more ideal details about online sports, research.


Post a Comment

Popular posts from this blog

OverTheWire[.com] Natas Walkthrough - JUST HINT, NO SPOILERS

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem

Asp.Net Ending Response options, Response.End() vs CompleteRequest()