Free Web Development Online Tutorials, Learn to Code
Web API Tutorial in C#, Learn Asp.net Core Web Api Development, Web API Security, Authentication, Authorization

Asp.net Core Web API Tutorial

This is Asp.net Core Web API Tutorial for Beginners and Professionals with some real-time examples. Please feel free to ask question, I will keep updating this tutorial with answer of your query.

RESTful Web Api using Asp.Net Core

In this tutorial you learn how to create Web API using asp.net core framework, like earlier version of Web API you will see many similarities and also learn many new things from asp.Net Core framework

We recommend you to take a look at some quick overview of Asp.net Core Framework, before starting with Web API

Create Asp.Net Core Web API

Now let’s start with Web API.
Open Visual Studio => Web => Asp.net Core Web Application => API => OK

asp.net core web api

Some default code generated with basic functionalities, good enough to get started with!

namespace WebTrainingRoom
{
    [Route("api/[controller]")]
    public class TestController : Controller
    {
        // GET: api/<controller>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
        // GET api/<controller>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }
        // POST api/<controller>
        [HttpPost]
        public void Post([FromBody]string value)
        {
        }
        // PUT api/<controller>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }
        // DELETE api/<controller>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}

https locally for asp.net core API

After creating the Asp.net Core Web API project when you run the project you may get some SSL error, it will try to run the project on https on your local environment, It tells you to create same environment set up as production. so either you can avoid by just disabling ssl setting in following code.

or you need to use "dev-certs", there is a dotnet tool built into .NET Core 2.1 to help with certs at dev time.
You need to run "dotnet dev-certs https --trust"
then running the app on localhost you get a pop up asking if you want to trust this localhost, you need to say yes.

expand properties=> launchSettings.json
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:20032",
//"sslPort": 44327
    }
}

Now you will be able to run the project simply and see how the string array is being returned from default get function

[HttpGet]
public IEnumerable<string> Get()
{
    return new string[] { "value1", "value2" };
}
//localhost:20034/api/test

This was a basic example of creating asp.net core web API on localhost, now we create an asp.net core web api with database methods including security and different serialization settings, exception handing etc.


Comment
Name
Email
Website
Subscribe
 
Asp.Net Web API Tutorial

C# web service interview questions