Click here to Skip to main content
15,558,630 members
Articles / Web Development / ASP.NET / ASP.NETvNext
Tip/Trick
Posted 9 Jun 2016

Stats

79.7K views
12 bookmarked

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)
11 Jun 2016CPOL
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 10:50
uscboy1-Oct-20 10:50 
GeneralMy vote of 5 Pin
dharmendra9910-Jun-18 20:32
dharmendra9910-Jun-18 20:32 
SuggestionAlternate way to define multiple actions. Pin
Amila Prabandhika Palahepitiya Gamage13-Jun-16 2:40
Amila Prabandhika Palahepitiya Gamage13-Jun-16 2:40 
QuestionWhere is post method???? Pin
Vijaykumar Vadnal10-Jun-16 2:00
Vijaykumar Vadnal10-Jun-16 2:00 
AnswerRe: Where is post method???? Pin
Kasun Koswattha10-Jun-16 3:32
Kasun Koswattha10-Jun-16 3:32 
I think you can follow the same approach for the posts also.
AnswerRe: Where is post method???? Pin
TheForceIsSharp10-Jun-16 13:16
TheForceIsSharp10-Jun-16 13: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.