Click here to Skip to main content
15,881,204 members
Articles / Web Development / ASP.NET / ASP.NETvNext
Tip/Trick

ASP.NET Core Web API, Multiple Get or Post Methods with Single Controller

Rate me:
Please Sign up or sign in to vote.
5.00/5 (15 votes)
10 Jun 2016CPOL 83.9K   12   6
ASP.NET Core Web API, Multiple Get or Post methods with single controller

Why?

I was assigned a duty to develop a RESTful API for my company. And I was like, why not try out the new .NET Core. So I started an MVC Web API project. No surprise, it was similar to .NET 4.5+. Single Get or Post method for each controller. But I wanted a service controller with several get methods for customer.

Let's get to it.

Instead of modifying the webapiconfig, the Route options is directly in the controller class.

C#
//
// Default generated controller
//

[Route("api/[controller]")
public class myApiController : Controller
{
    [HttpGet]
    public string GetInfo()
    {
        return "Information";
    }
}

//
//A little change would do the magic
//

[Route("api/[controller]/[action]")]
public class ServicesController : Controller
{
    [HttpGet]
    [ActionName("Get01")]
    public string Get01()
    {
        return "GET 1";
    }

    [HttpGet]
    [ActionName("Get02")]
    public string Get02()
    {
        return "Get 2";
    }
    
    [HttpPost]
    [ActionName("Post01")]
    public HttpResponseMessage Post01(MyCustomModel01 model)
    {
        if (!ModelState.IsValid)
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        
        //.. DO Something ..
        return Request.CreateResonse(HttpStatusCode.OK, "Optional Message");
    }
    
    [HttpPost]
    [ActionName("Post02")]
    public HttpResponseMessage Post02(MyCustomModel02 model)
    {
        if (!ModelState.IsValid)
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        
        //.. DO Something ..
        return Request.CreateResonse(HttpStatusCode.OK, "Optional Message");
    }


}

//

That's it. Hope this will help someone.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThis was very helpful. Pin
uscboy1-Oct-20 9:50
uscboy1-Oct-20 9:50 
GeneralMy vote of 5 Pin
dharmendra9910-Jun-18 19:32
dharmendra9910-Jun-18 19:32 
SuggestionAlternate way to define multiple actions. Pin
Amila Prabandhika Palahepitiya Gamage13-Jun-16 1:40
Amila Prabandhika Palahepitiya Gamage13-Jun-16 1:40 
QuestionWhere is post method???? Pin
Vijaykumar Vadnal10-Jun-16 1:00
Vijaykumar Vadnal10-Jun-16 1:00 
AnswerRe: Where is post method???? Pin
Kasun Koswattha10-Jun-16 2:32
Kasun Koswattha10-Jun-16 2:32 
AnswerRe: Where is post method???? Pin
TheForceIsSharp10-Jun-16 12:16
TheForceIsSharp10-Jun-16 12:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.