Custom method names in ASP.Net Web API

Last Reply one year ago By dharmendr

Posted one year ago

Hi,

I am having below API class.

[RoutePrefix("Admin")]
public class Admin_SegmentController : ApiController
{
    [Route("Segment")]
    public List<SegmentBO> Get()
    {
        return SQLUtility.Admin.Master.Segment.GetSegmentDetails();
    }
}

So I am able to achieve the requirement of having alias like http://localhost:64297/admin/segment.

I am having other methods in my class like below.

        public SegmentBO Get(int ID)
        {
            return SQLUtility.Admin.Master.Segment.GetSegmentDetails(ID);
        }

        // POST: api/Segment
        public int Post([FromBody]SegmentBO SegmentObj)
        {
            return SQLUtility.Admin.Master.Segment.InsertSegmentDetails(SegmentObj);
        }

        // PUT: api/Segment
        public int Put([FromBody]SegmentBO SegmentObj)
        {
            return SQLUtility.Admin.Master.Segment.UpdateSegmentDetails(SegmentObj);
        }

        // DELETE: api/Segment
        public int Delete(SegmentBO SegmentObj)
        {
            return SQLUtility.Admin.Master.Segment.DeleteSegmentDetails(SegmentObj);
        }

How to give aliases for the above methods and what are best practices/ways to do that (like can we able to put variables in WebApiConfig.cs and use in controller classes, is this possible). Thanks in advance.