Reputation: 175
I Have a C# WinForms application and in a form I want to consume (Get) some data from an ASP Web API application.
in Web API I have a controller
named Session inside this controller I have two methods as shown below :
public int Create()
{
Random random = new Random();
int New_ID = random.Next();
return New_ID ;
}
public string Get()
{
return "Catch this data";
}
So the problem is when I use browser URL (For testing) to access to controller (Session)
URL : http://localhost:52626/api/Session
And when I want to access to controller (Session) especially Create Function
URL : http://localhost:52626/api/Session/Create
I got
The global question is how can I create my own methods and access to it without depending on Get ?
Upvotes: 1
Views: 1032
Reputation: 885
The browser always fires a GET. Web-API by default selects the method to be called using the HTTP verb (GET in your case) of request. Hence it always lands in "Get" method. To make Web-API point to your method you need to define a route. Use below:
[Route("api/session/Create")]
[HttpGet]
public int Create()
{
Random random = new Random();
int New_ID = random.Next();
return New_ID;
}
To better understand route based selection, read this : https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-and-action-selection
Upvotes: 2