C# .NET Core 6 web API with JavaScript, using the Fetch API — Coding for Entrepreneurs

Spencer Thomason
2 min readJun 18, 2022

This is our series about Coding for Entrepreneurs! I am a firm believer that even if you aren’t the one doing the coding in your organization, that learning to code will help you to more effectively manage your team and your product! So I think every Entrepreneur should learn to code!

Call an ASP.NET Core web API with JavaScript

This tutorial sho041ws how to call an ASP.NET Core web API with JavaScript, using the Fetch API. So let’s dig in!

Code Sample for today: https://github.com/slthomason/StartupHakk/tree/main/04_Todo_App_WebApi_from_Js

Call the web API with JavaScript
In this section, you’ll add an HTML page containing forms for creating and managing to-do items. Event handlers are attached to elements on the page. The event handlers result in HTTP requests to the web API’s action methods. The Fetch API’s fetch function initiates each HTTP request.

The fetch function returns a Promise object, which contains an HTTP response represented as a Response object. A common pattern is to extract the JSON response body by invoking the json function on the Response object. JavaScript updates the page with the details from the web API’s response.

The simplest fetch call accepts a single parameter representing the route. A second parameter, known as the init object, is optional. init is used to configure the HTTP request.

It ultimately depends on the technology you’re building on but in this series, we’ll be working on .Net/.Net CORE. For that, you either want to use Microsoft Visual Studio or Microsoft Visual Studio Code. Both of them can be downloaded for free from the official website for both Windows and macOS.

You can download both of them and see which one suits your needs. In this video, we’ll create a new project and create a simple Hello World application. It can act as a guide to get familiarized with different aspects of visual studio/code such as the solution explorer and properties.

Visual Studio vs Visual Studio Code

You can think of Visual Studio as a cookie-cutter solution that is jampacked with features and functionality. As Visual Studio is much more powerful, it works in most situations out of the box whereas Visual Studio Code can be considered a Lite version of it with lots of addons available which can ultimately make the two nearly identical. Some might even consider it as a “glorified text editor”. In comparison, Visual Studio does require more knowledge to be utilized to its true potential unlike Visual Studio Code which is more beginner-friendly however, Visual Studio does offer more precise control over everything. At the end of the day, it all comes down to personal preference.

--

--