How to Customize ASP.NET Web API Routing For Methods in APIController Class

If you are familiar with WebAPI Controller and MVC4, then you’ll know what API controller class. In a practical scenario, the developer may implement the WebAPI if he or she wants to write his or her own method and want to map it with HTTP Requests like GET|POST. The example below is what APIController class can do:

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)
{
}
}

 

Read the rest of the tutorial here: http://www.dotnetcurry.com/ShowArticle.aspx?ID=854

Leave a Reply

Your email address will not be published. Required fields are marked *