0

The below link shows how to creat ApiController.

http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

So how we can create a new method in controller with different paramaeter.

Something like this.

public bool SaveIttoDB(string name, string email, DateTime dob, int id)
{
    // code here
}

How we can access this method using URL?

Can anubody help me on this.

tereško
  • 58,060
  • 25
  • 98
  • 150
PaRsH
  • 1,320
  • 4
  • 28
  • 56

2 Answers2

1

The URL for that type of action (assuming it's a HttpGet) would look like

http://domain/controller/SaveItToDb/name?email=value&dob=value&id=value

Or

http://domain/controller/SaveItToDb?name=value&email=value&dob=value&id=value
James
  • 80,725
  • 18
  • 167
  • 237
  • James, is the url is correct??? Don't we need to pass the value for name also??? http: //domain/controller/SaveItToDb?name=value&email=value&dob=value&id=value Is this correct url??? – PaRsH Jun 25 '13 at 07:36
  • @PaRsH the value for `name` *is* being passed - in both URLs. – James Jun 25 '13 at 07:38
  • I don't think this would work for a WebApi controller since it expects the method names to start woth Get or Post? Unless you configure it otherwise, ofcourse – Jordy Langen Jun 25 '13 at 07:39
  • Argumentation for my comment: http://stackoverflow.com/questions/9569270/custom-method-names-in-asp-net-web-api – Jordy Langen Jun 25 '13 at 07:39
  • We dont have any post method in our controlle. We have to name the Action something like GetSaveItIoDB isn't it? – PaRsH Jun 25 '13 at 07:41
  • @JordyLangen I was focusing more on the actual question "*How can we access this method using URL?*" - I was assuming the OP had their route configured. – James Jun 25 '13 at 07:42
0

This would work:

Note, I'm converting a POST action to a GET action for demonstration purposes.

public bool GetSaveItToDB(string name, string email, DateTime dob, int id)
{
    // code here
}

You would call it using this url:

http://localhost/api/MyWebApiControllerName?name=John&email=john@doe.com&dpob=2012-13-05&id=1

It would normally be a POST:

public bool PostItToDB(string name, string email, DateTime dob, int id)
{
    // code here
}

You would then provide the parameters as POST parameters.

Jordy Langen
  • 3,591
  • 4
  • 23
  • 32