How to get started with Web API in .ASP.NET Core

If you want to know what is Web API, I have an article on that which can be accessed here.

Let’s dive in and see how to create a bare-bones Web API project from inside Visual Studio 2017. To do that you can use the Community edition, Professional or the Enterprise edition.

Fire up Visual Studio. Navigate to File -> New Project

Select ASP.NET Core Web Application. There isn’t a Web API project per se, but, you’ll see that in just a moment. Next, give this project a Name and click OK.

Here, you have the option to select one of the templates. In our case, we’ll pick Web API. Notice, the item circled in red. This is the platform where we are targeting “ASP.NET Core 2.1”. Now click OK.

Visual Studio will create and add all the necessary files for you. This consists of the following:

  •  ValuesController class
  • Program.cs
  • Startup.cs

One thing to keep in mind though is the project adds ControllerBase as the base class that ValuesController derives from. This is significant because the ControllerBase does not provide a View support. In addition, it provides access to several properties and methods.

Next, I’ll modify the ActionResult methods for Get verb of the ValuesController class ever so slightly. This Get() method will return us an IEnumerable of type String or in words, it’ll give us a collection of strings without passing any parameters to it.

// GET api/values
        [HttpGet]
        public ActionResult<IEnumerable> Get()
        {
            return new string[] { "This", "is", "a", "blog", "test!" };
        }
// GET api/values/5
        [HttpGet("{id}")]
        public ActionResult Get(int id)
        {
            string retVal = string.Empty;

            switch (id)
            {
                case 1:
                    retVal = "Gather Project Requirements";
                    break;
                case 2:
                    retVal = "Design App";
                    break;
                case 3:
                    retVal = "Write Code";
                    break;
                case 4:
                    retVal = "Test App";
                    break;
                case 5:
                    retVal = "Deploy App";
                    break;
                default:
                    retVal = "Project Status!";
                    break;
            }
            return retVal;
        }

Now, build your project and run the app in your favorite browser. You can either use your browser for testing or simply copy the URL from the browser’s address bar and paste it in Postman or Fiddler.

I am using Postman for testing. Postman is a complete API development environment for API developers. You can download Postman from here. Postman also lets you save your requests for future testing.

In the image below, the Get() method returns a 200 OK response, which serializes the string as JSON.

To test the Get ActionResult by ID, simply provide the ID from 1 to 5 and see the output circled in red. Here, we are following the convention i.e. api/{controller}/{id}

In the next exercise, we will extend this solution by adding Model and Controller classes.

So stay tuned!!

About Obi Oberoi

Obi Oberoi is an Independent Consultant, Developer and a Life-Long student of continuous learning. Obi enjoys to code, read, and hang out with techies and geeks!
This entry was posted in .NET, Web Services. Bookmark the permalink.