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.
In this tutorial you learn how to create asp.net core Web API, 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
Now let’s start with Web API.
Open Visual Studio => Web => Asp.net Core Web Application => API => OK
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) { } } }
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.
"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.
You may also learn Asp.net Core Web API [part 2] Asp.net Core 3.1 Web API Example