0

Hi I am developing one restfull wep api application. I am new to the world of web api and little confused about http verbs. My tasks is to write services using web api2. I have one table in sql server and i am trying to do basic crud operation around this table. I want to send data in json format and return data as json. For example http://localhost:26079/api/User_Creation/1 returns data in json format as expected. My task is to host above method in iis so that anyone can access that method to retrieve data. I am confused suppose if i want to insert some data to db then what would be the method? I have below code in controllera and i am able to insert data.

            public void Post(Noor_Users users)
            {
                if (ModelState.IsValid)
                {
                    entityObject.Noor_Users.Add(users);
                    int result = entityObject.SaveChanges();
                }
            }

When i insert data my url will be http://localhost:26079/ but how can i expose my insert data method to outside world? My requirement is as follows.

URL:/user_creation
method:post
Request:parameters such as fname,lname as json
Response:0 for success 1 for failure and data(unique id assigned to each user)

may i get some help on this? Thank for consideration.

2
  • 1
    when you create a new WebApi project you will be having auto generated controllers like Account,Home. Have a look at it you can understand abotu the verbs Commented Jan 12, 2017 at 6:18
  • Thank you for your reply. I have one doubt. I am using angularjs as front end. So for example i have public async Task<IHttpActionResult> Register(RegisterBindingModel model) this method id used to save data. How can i call this method from angularjs? Commented Jan 12, 2017 at 6:35

2 Answers 2

2

When considering REST it is important to understand and design it as you are taking actions against a resource at the location, and not like making a remote function call.

So I would suggest to have API as - http://localhost:26079/api/User

instead of - http://localhost:26079/api/User_Creation

In order to adhere to REST principals.

Doing this I am very clearly stating that the user of the api will be able to perform operations on the resource (which is a User in this case) using different verbs viz. GET, POST, PUT and DELETE.

See some examples on using the API -

Please note here that we are using just a single endpoint to perform different operations on our User by changing different HTTP verbs.

The default asp.net web api template gives some good hint on how to declare different verb methods e.g. -

public class UserController : ApiController
{
    // GET api/<controller>/1
    public User Get(int id)
    {
    }

    // POST api/<controller>
    public void Post([FromBody]User user)
    {
    }

    // PUT api/<controller>/1
    public void Put(int id, [FromBody]User user)
    {
    }

    // DELETE api/<controller>/1
    public void Delete(int id)
    {
    }
}

There is some good information here and here on designing restful api.

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

3 Comments

thanks for your post. I got my basic understandings in a clean way. Still I have few points. Instead of void can i have other return types? can i have multiple post in one controller? If so how to distinguish between them?
@NiranjanGodbole: Added a discussion and a series on some good stuff on api design. Hope that would help :)
Yes i will go through. Thank you.
0

If you want to call your emthod from Angular, you can just use $http.post() method:

$http.post('/someUrl', data, config).then(successCallback, errorCallback);

Here you can find more information.

1 Comment

Thank you for your response.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.