Menu

Blogs

We Care Through What We Share
  • This blog post will help us to getting started WEBAPI development on the ASP.Net core 1.0 with Ubuntu 16.04. We are going to use the Visual Studio Code for editor.

    It is very easy using Visual Studio Code with Yeoman generators. This will help us to getting started with WEBAPI development without need of the Visual Studio.

    Before starting ensure Yoeman, Visual studio code is installed. https://code.visualstudio.com/

    If Yoeman is not not installed then you can read the complete guide here to install it. https://docs.asp.net/en/latest/client-side/yeoman.html

    We will start with the command line using Yoeman generators to create the WEBAPI project for us. Look at this screenshot.

    These are the steps you need to follow.

    1. 1. Create the directory for your project files
    2. 2. Run the yo aspnet command to invoke Yoemen generator for ASPNET
    3. 3. Generator ask to select the project type. We need to choose the “WEB API Application”
    4. 4. I named it as “demoapp” and hit enter
    5. 5. Generator will list all the generated files.
    6. 6. We need to run the “dotnet restore” command to restore the libraries

    Now, open the Visual Studio Code and open the directory where the project is. It will look like this.

    Program.cs is the starting point of the ASP.Net Core apps so it have all the required configuration and startup items. This is the code of it.

    using System.IO;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.Extensions.Configuration;
    
    namespace demoapp
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                var config = new ConfigurationBuilder()
                    .AddCommandLine(args)
                    .AddEnvironmentVariables(prefix: "ASPNETCORE_")
                    .Build();
                
                var host = new WebHostBuilder()
                    .UseConfiguration(config)
                    .UseKestrel()
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseIISIntegration()
                    .UseStartup()
                    .Build();
    
                host.Run();
            }
        }
    }
    
    

    You can also see that WebHostBuilder class, which is responsible for host the application and other configurations. Microsoft has introduced the new cross plaform webserver called “Kestrel”

    Now, we will create a new model class. I have created the product model class with following.

    namespace demoapp.Model
    {
        public class Student
        {
            public int StudentId {get;set;}
            public string FirstName{get;set;}
            public string LastName {get;set;}
        }
    }
    

    This is the get method code.

    using System.Collections.Generic;
    using Microsoft.AspNetCore.Mvc;
    using demoapp.Model;
    
    namespace demoapp.Controllers
    {
        [Route("api/]controller[")]
        public class StudentsController : Controller
        {
            // GET api/values
            [HttpGet]
            public IEnumerable Get()
            {
                var students = new List
               {
                   new Student {StudentId = 1,FirstName = "Little",LastName = "Johny"},
                   new Student {StudentId = 2,FirstName = "Little",LastName = "Mary"}
               };
                return students;
            }
        }
    }
    

    This is the output of GET API which returns students.

Copyright © Toshal Infotech 2018, All Rights Reserved.