1

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

I got enter image description here

And when I want to access to controller (Session) especially Create Function

URL : http://localhost:52626/api/Session/Create

I got

enter image description here

The global question is how can I create my own methods and access to it without depending on Get ?

2
  • A URL you have client doing a Request and a getting back a Response. Your controller is a Server which accepts the request and returns the response. Commented Sep 28, 2020 at 17:58
  • You need to define a route to each method Commented Sep 28, 2020 at 18:05

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.