How to implement WebAPI in MVC

Answer

Below are the steps to implement WebAPI:

Step 1: Create the project using the WebAPI template.

Figure: Implement WebAPI in MVC

Step 2: Once you have created the project you will notice that the controller now inherits from ApiController and you can now implement POST, GET, PUT, and DELETE methods of the HTTP protocol.

public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
    // GET api/values/5
    public string Get(int id)
    {
        return "value";
    }
    // POST api/values
    public void Post([FromBody]string value)
    {
    }
    // PUT api/values/5
    public void Put(int id, [FromBody]string value)
    {
    }
    // DELETE api/values/5
    public void Delete(int id)
    {
    }
} 

Step 3: If you make an HTTP GET call you should get the below results:

Figure: HTTP

All asp.net-mvc Questions

Ask your interview questions on asp-net-mvc

Write Your comment or Questions if you want the answers on asp-net-mvc from asp-net-mvc Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---