How YOU can build a Mock REST API based on JSON for ASP .NET and minimal API

Published Feb 21 2022 05:05 PM 2,586 Views
Microsoft

TLDR; this article describes how to create a Mock API from a JSON file for minimal API in ASP .NET

 

What and why Mock APIs

To mock something means you respond with fake data, that data can be in-memory, from a file or some kind of tool generating a bunch of endpoints. There are some reasons why mocking an API could be a good idea:

  • Different teams work at different speeds. Let's say your app is built by two different teams, or developers and one is faster than the other. That's when it's handy to rely on a mocked API.
  • You start with the frontend first. Your team/developer have decided to build a full vertical and starts with the frontend and slowly work their way towards the backend and the data source.

Ok, so we established there might be a need to mock your API. So how do we do it? You want to be able to specify the data you want to mock and there are some formats out there that makes sense to have such mocked data in like JSON, XML or CSV perhaps. For the sake of this article, we will go with JSON

 

Planning our project, what we need to do

Ok, so high-level, we need to do the following:

  • Create a file in JSON, containing our routes and the response. We imagine the JSON file looking something like so:

 

  {
      "Products": [
        {
          "Id": 1,
          "Name": "Mock"
        },
        {
          "Id": 2,
          "Name": "Second Mock"
        }
      ]
   }

 

 
  • What routes do we want? A good API should implement GET, POST, PUT and DELETE to support a RESTful approach.
  • Responding to changes. So what should happen if the user actually calls POST, PUT or DELETE? Reasonably, the mocked file should change.

Ok, so we know high-level what we need, and how things should behave, let's see if we can choose our technical approach next.

 

Approach - let's create the solution

The normal way to setup routes, in Minimal API, is to call code like so:

 

app.MapGet("/", () => "Hello World!");

 

 

 

By calling MapGet() we create a route to "/" that when called responds with "Hello World". For the sake of our API, we will have to call MapGet(), MapPost(), MapPut() and MapDelete().

Here be dragons. Many of you, I'm sure, are used to working with JSON in a typed manor, meaning you are likely to create types for your classes and rely on methods like Deserialize() and Serialize(). That's a great approach, however, for a mocked API that doesn't even exist yet, this code doesn't rely on any of that :)

Defining the routes, making it loosely coupled

It would be neat if these routes were loosely coupled code that we could just bring in, when developing, and removed when we are live with our app.

When app.MapGet() was called, it invoked an instance of the class WebApplication. By creating an extension method on said class, we have a way an approach to add code in a way that it's nicely separated. We also need a static class to put said extension method in. That means our code starting out should look something like so:

 

public static class RouteMiddlewareExtensions 
{
  public static WebApplication UseExtraRoutes(this WebApplication app)
  {
  }
}

 

 

 

Exercise - Read from a mock file, and add support for GET

Ok, we know how we are starting, a static class and an extension method, so let's make that happen:

  1. Run dotnet new, to generate a new minimal API project

 

   dotnet new web -o MyApi -f net6.0
   cd Myapi

 

 
  1. Create a file MockMiddleware.cs and give it the following code:

 

   using System.Text.Json;
   using System.Text.Json.Nodes;

   public static class RouteMiddlewareExtensions
   {
      public static WebApplication UseExtraRoutes(this WebApplication app)
      { 
      }
   }

 

 
  1. Add code to read a JSON file into a JSON representation:

 

 var writableDoc = JsonNode.Parse(File.ReadAllText("mock.json"));

 

 

Note the choice of JsonNode, this is so we can make the JSON doc writable, which we will need for POST, PUT and DELETE later on.

  1. Create the file mock.json and give it the following content:

 

   {
      "Products": [
        {
          "Id": 1,
          "Name": "Mock"
        },
        {
          "Id": 2,
          "Name": "Second Mock"
        }
      ],
      "Orders": [
        {
          "Id": 1,
          "Name": "Order1"
        },
        {
          "Id": 2,
          "Name": "Second Order"
        }
      ]
    }

 

 

Add GET

Let's support our first HTTP verb, GET.

  1. Add the following code:

 

   foreach(var elem in writableDoc?.Root.AsObject().AsEnumerable()) {
      var arr = elem.Value.AsArray();
      app.MapGet(string.Format("/{0}", elem.Key), () => elem.Value.ToString());
   }

 

 

In the above code, we navigate into the root object. Then, we convert it to an object representation and starts iterating over the keys, according to the mock file, that means Products and Orders. Lastly, we setup the route and the callback, the route is at elem.Key and the value we want to return is at elem.Value.

  1. In the file Program.cs add the following line:

 

   app.UseExtraRoutes();

 

 

The preceding code will ensure our routes are added to the app.

  1. Run dotnet run, to run the app

 

   dotnet run

 

 
  1. Navigate to the port indicated in the console output and navigate to /products and /orders, they should both show an output

Add GET by id

Ok, you got the basic GET case to work, what about filtering the data with parameter. Using /products/1, should just return one record back. How do we do that?

  1. Add the following code in the foreach loop in MockMiddlware.cs:

 

app.MapGet(string.Format("/{0}", elem.Key) + "/{id}", (int id) =>
      {
        var matchedItem = arr.SingleOrDefault(row => row
          .AsObject()
          .Any(o => o.Key.ToLower() == "id" && int.Parse(o.Value.ToString()) == id)
        );
        return matchedItem;
      });

 

 

The above code is iterating over the rows for a specific route and looks for an id property that matches our {id} pattern. The found item is returned.

  1. Run dotnet run to test out this code:

 

  dotnet run

 

 
  1. Navigate to /products/1, you should see the following JSON output:

 

   {

      "Id": 1,
      "Name": "Mock"
   }

 

 

Great, we got it to work.

 

Exercise - write data

Now that we can read data from our mock API, lets tackle writing data. The fact that we were JsonNode.Parse() in the beginning makes it possible for us to use operations on the JsonNode instance. In short, our approach will be:

  • find the specific place in the JsonNode, that represents our mock data, and change it
  • save down the whole JsonNode instance to our mock.json. If the user uses an operation to change the data, that should be reflected in the Mock file.

Add POST

To implement this route, we will use MapPost() but we can't just give it a typed object in the callback for the route, because we don't know what it looks like. Instead, we will use the request object, read the body and add that to the JsonNode.

  1. Add following code to support POST:

 

  app.MapPost(string.Format("/{0}", elem.Key), async (HttpRequest request) => {
        string content = string.Empty;
        using(StreamReader reader = new StreamReader(request.Body))
        {
          content = await reader.ReadToEndAsync();
        }
        var newNode = JsonNode.Parse(content);
        var array = elem.Value.AsArray();
        newNode.AsObject().Add("Id", array.Count() + 1);
        array.Add(newNode);

        File.WriteAllText("mock.json", writableDoc.ToString());
        return content;
      });

 

 

In the above code, we have request as input parameter to our route handler function.

 

 app.MapPost(string.Format("/{0}", elem.Key), async (HttpRequest request) => {});

 

 

 

Then we read the body, using a StreamReader.

 

 using(StreamReader reader = new StreamReader(request.Body))
 {
   content = await reader.ReadToEndAsync();
 }

 

 

 

Next, we construct a JSON representation from our received BODY:

 

var newNode = JsonNode.Parse(content);

 

 

 

This is followed by locating the place to insert this new JSON and adding it:

 

var array = elem.Value.AsArray();
newNode.AsObject().Add("Id", array.Count() + 1);
array.Add(newNode);

 

 

 

Lastly, we update the mock file and respond something back to the calling client:

 

File.WriteAllText("mock.json", writableDoc.ToString());
return content;

 

 

 

Add DELETE

To support deletion, we need a very similar approach to how we located an entry by id parameter. We also need to locate where to delete in the JsonObject.

  1. Add the following code to support delete:

 

  app.MapDelete(string.Format("/{0}", elem.Key) + "/{id}", (int id) => {
        var matchedItem = arr
         .Select((value, index) => new{ value, index})
         .SingleOrDefault(row => row.value
          .AsObject()
          .Any(o => o.Key.ToLower() == "id" && int.Parse(o.Value.ToString()) == id)
        );
        if (matchedItem != null) {
          arr.RemoveAt(matchedItem.index);
          File.WriteAllText("mock.json", writableDoc.ToString());
        }

        return "OK";
      });

 

 

First, we find the item in question, but we also make sure that we know what the index of the found item is. We will use this index later to remove the item. Hence, we get the following code:

 

  var matchedItem = arr
         .Select((value, index) => new{ value, index})
         .SingleOrDefault(row => row.value
          .AsObject()
          .Any(o => o.Key.ToLower() == "id" && int.Parse(o.Value.ToString()) == id)
        );

 

 

 

Our matchedItem now contains either NULL or an object that has an index property. Using this index property, we will be able to perform deletions:

 

   if (matchedItem != null) {
     arr.RemoveAt(matchedItem.index);
     File.WriteAllText("mock.json", writableDoc.ToString());
   }

 

 

 

To test writes, use something like Postman or Advanced REST client, it should work.

Add route info

We're almost done, as courtesy towards the programmer using this code, we want to print out what routes we have and support so it's easy to know what we support.

  1. Add this code, just at the start of the method UseExtraRoutes():

 

   // print API
    foreach (var elem in writableDoc?.Root.AsObject().AsEnumerable()){
      Console.WriteLine(string.Format("GET /{0}", elem.Key.ToLower()));
      Console.WriteLine(string.Format("GET /{0}", elem.Key.ToLower()) + "/id");
      Console.WriteLine(string.Format("POST /{0}", elem.Key.ToLower()));
      Console.WriteLine(string.Format("DELETE /{0}", elem.Key.ToLower()) + "/id");
      Console.WriteLine(" ");
    }

 

 

That's it, that's all we intend to implement. Hopefully, this is all useful to you and you will be able to use it next you just want an API up and running that you can build a front-end app off of.

Full code

If you got lost at any point, here's the full code:

Program.cs

 

using Mock;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");
app.UseExtraRoutes(); // this is where our routes get added

app.Run();

 

 

 

MockMiddleware.cs

 

using System.Text.Json;
using System.Text.Json.Nodes;

namespace Mock;

public static class RouteMiddlewareExtensions 
{
  public static WebApplication UseExtraRoutes(this WebApplication app)
  {
    var writableDoc = JsonNode.Parse(File.ReadAllText("mock.json"));

    // print API
    foreach (var elem in writableDoc?.Root.AsObject().AsEnumerable()){
      Console.WriteLine(string.Format("GET /{0}", elem.Key.ToLower()));
      Console.WriteLine(string.Format("GET /{0}", elem.Key.ToLower()) + "/id");
      Console.WriteLine(string.Format("POST /{0}", elem.Key.ToLower()));
      Console.WriteLine(string.Format("DELETE /{0}", elem.Key.ToLower()) + "/id");
      Console.WriteLine(" ");
    }

    // setup routes
    foreach(var elem in writableDoc?.Root.AsObject().AsEnumerable()) {
      var arr = elem.Value.AsArray();
      app.MapGet(string.Format("/{0}", elem.Key), () => elem.Value.ToString());
      app.MapGet(string.Format("/{0}", elem.Key) + "/{id}", (int id) =>
      {
        var matchedItem = arr.SingleOrDefault(row => row
          .AsObject()
          .Any(o => o.Key.ToLower() == "id" && int.Parse(o.Value.ToString()) == id)
        );
        return matchedItem;
      });      
      app.MapPost(string.Format("/{0}", elem.Key), async (HttpRequest request) => {
        string content = string.Empty;
        using(StreamReader reader = new StreamReader(request.Body))
        {
          content = await reader.ReadToEndAsync();
        }
        var newNode = JsonNode.Parse(content);
        var array = elem.Value.AsArray();
        newNode.AsObject().Add("Id", array.Count() + 1);
        array.Add(newNode);

        File.WriteAllText("mock.json", writableDoc.ToString());
        return content;
      });
      app.MapPut(string.Format("/{0}", elem.Key), () => {
        return "TODO";
      });
      app.MapDelete(string.Format("/{0}", elem.Key) + "/{id}", (int id) => {

        var matchedItem = arr
         .Select((value, index) => new{ value, index})
         .SingleOrDefault(row => row.value
          .AsObject()
          .Any(o => o.Key.ToLower() == "id" && int.Parse(o.Value.ToString()) == id)
        );
        if (matchedItem != null) {
          arr.RemoveAt(matchedItem.index);
          File.WriteAllText("mock.json", writableDoc.ToString());
        }

        return "OK";
      });

    };

    return app;
  }
}

 

 

 

Update - homework

For your homework, see if you can implement PUT. :)

 

Summary

I took you through a journey of implementing a Mock API for minimal APIs. Hopefully, you found this useful and can use it in a future project.

Here's a repo for the code repo

Please comment if you want me to make this into either a tool or a NuGet package :)

%3CLINGO-SUB%20id%3D%22lingo-sub-3196039%22%20slang%3D%22en-US%22%3EHow%20YOU%20can%20build%20a%20Mock%20REST%20API%20based%20on%20JSON%20for%20ASP%20.NET%20and%20minimal%20API%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3196039%22%20slang%3D%22en-US%22%3E%3CBLOCKQUOTE%3E%0A%3CP%3ETLDR%3B%20this%20article%20describes%20how%20to%20create%20a%20Mock%20API%20from%20a%20JSON%20file%20for%20minimal%20API%20in%20ASP%20.NET%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CH2%20id%3D%22toc-hId-367031515%22%20id%3D%22toc-hId-367031573%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--1440422948%22%20id%3D%22toc-hId--1440422890%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23what-and-why-mock-apis%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22what-and-why-mock-apis%22%3E%3C%2FA%3EWhat%20and%20why%20Mock%20APIs%3C%2FH2%3E%0A%3CP%3ETo%20mock%20something%20means%20you%20respond%20with%20fake%20data%2C%20that%20data%20can%20be%20in-memory%2C%20from%20a%20file%20or%20some%20kind%20of%20tool%20generating%20a%20bunch%20of%20endpoints.%20There%20are%20some%20reasons%20why%20mocking%20an%20API%20could%20be%20a%20good%20idea%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EDifferent%20teams%20work%20at%20different%20speeds%3C%2FSTRONG%3E.%20Let's%20say%20your%20app%20is%20built%20by%20two%20different%20teams%2C%20or%20developers%20and%20one%20is%20faster%20than%20the%20other.%20That's%20when%20it's%20handy%20to%20rely%20on%20a%20mocked%20API.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EYou%20start%20with%20the%20frontend%20first%3C%2FSTRONG%3E.%20Your%20team%2Fdeveloper%20have%20decided%20to%20build%20a%20full%20vertical%20and%20starts%20with%20the%20frontend%20and%20slowly%20work%20their%20way%20towards%20the%20backend%20and%20the%20data%20source.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EOk%2C%20so%20we%20established%20there%20might%20be%20a%20need%20to%20mock%20your%20API.%20So%20how%20do%20we%20do%20it%3F%20You%20want%20to%20be%20able%20to%20specify%20the%20data%20you%20want%20to%20mock%20and%20there%20are%20some%20formats%20out%20there%20that%20makes%20sense%20to%20have%20such%20mocked%20data%20in%20like%20JSON%2C%20XML%20or%20CSV%20perhaps.%20For%20the%20sake%20of%20this%20article%2C%20we%20will%20go%20with%20JSON%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1047089885%22%20id%3D%22toc-hId-1047089943%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--760364578%22%20id%3D%22toc-hId--760364520%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23planning-our-project-what-we-need-to-do%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22planning-our-project-what-we-need-to-do%22%3E%3C%2FA%3EPlanning%20our%20project%2C%20what%20we%20need%20to%20do%3C%2FH2%3E%0A%3CP%3EOk%2C%20so%20high-level%2C%20we%20need%20to%20do%20the%20following%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3ECreate%20a%20file%20in%20JSON%3C%2FSTRONG%3E%2C%20containing%20our%20routes%20and%20the%20response.%20We%20imagine%20the%20JSON%20file%20looking%20something%20like%20so%3A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%20%20%7B%0A%20%20%20%20%20%20%22Products%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22Id%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%20%20%22Name%22%3A%20%22Mock%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22Id%22%3A%202%2C%0A%20%20%20%20%20%20%20%20%20%20%22Name%22%3A%20%22Second%20Mock%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%5D%0A%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EWhat%20routes%20do%20we%20want%3F%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EA%20good%20API%20should%20implement%20GET%2C%20POST%2C%20PUT%20and%20DELETE%20to%20support%20a%20RESTful%20approach.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EResponding%20to%20changes.%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3ESo%20what%20should%20happen%20if%20the%20user%20actually%20calls%20POST%2C%20PUT%20or%20DELETE%3F%20Reasonably%2C%20the%20mocked%20file%20should%20change.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EOk%2C%20so%20we%20know%20high-level%20what%20we%20need%2C%20and%20how%20things%20should%20behave%2C%20let's%20see%20if%20we%20can%20choose%20our%20technical%20approach%20next.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1727148255%22%20id%3D%22toc-hId-1727148313%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--80306208%22%20id%3D%22toc-hId--80306150%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23approach-lets-create-the-solution%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22approach-lets-create-the-solution%22%3E%3C%2FA%3EApproach%20-%20let's%20create%20the%20solution%3C%2FH2%3E%0A%3CP%3EThe%20normal%20way%20to%20setup%20routes%2C%20in%20Minimal%20API%2C%20is%20to%20call%20code%20like%20so%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eapp.MapGet(%22%2F%22%2C%20()%20%3D%26gt%3B%20%22Hello%20World!%22)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EBy%20calling%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EMapGet()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewe%20create%20a%20route%20to%20%22%2F%22%20that%20when%20called%20responds%20with%20%22Hello%20World%22.%20For%20the%20sake%20of%20our%20API%2C%20we%20will%20have%20to%20call%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EMapGet()%3C%2FCODE%3E%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EMapPost()%3C%2FCODE%3E%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EMapPut()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EMapDelete()%3C%2FCODE%3E.%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3EHere%20be%20dragons.%20Many%20of%20you%2C%20I'm%20sure%2C%20are%20used%20to%20working%20with%20JSON%20in%20a%20typed%20manor%2C%20meaning%20you%20are%20likely%20to%20create%20types%20for%20your%20classes%20and%20rely%20on%20methods%20like%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EDeserialize()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3ESerialize()%3C%2FCODE%3E.%20That's%20a%20great%20approach%2C%20however%2C%20for%20a%20mocked%20API%20that%20doesn't%20even%20exist%20yet%2C%20this%20code%20doesn't%20rely%20on%20any%20of%20that%20%3A)%3C%2Fimg%3E%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CH3%20id%3D%22toc-hId-610255266%22%20id%3D%22toc-hId-610255324%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23defining-the-routes-making-it-loosely-coupled%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22defining-the-routes-making-it-loosely-coupled%22%3E%3C%2FA%3EDefining%20the%20routes%2C%20making%20it%20loosely%20coupled%3C%2FH3%3E%0A%3CP%3EIt%20would%20be%20neat%20if%20these%20routes%20were%20loosely%20coupled%20code%20that%20we%20could%20just%20bring%20in%2C%20when%20developing%2C%20and%20removed%20when%20we%20are%20live%20with%20our%20app.%3C%2FP%3E%0A%3CP%3EWhen%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eapp.MapGet()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewas%20called%2C%20it%20invoked%20an%20instance%20of%20the%20class%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EWebApplication%3C%2FCODE%3E.%20By%20creating%20an%20extension%20method%20on%20said%20class%2C%20we%20have%20a%20way%20an%20approach%20to%20add%20code%20in%20a%20way%20that%20it's%20nicely%20separated.%20We%20also%20need%20a%20static%20class%20to%20put%20said%20extension%20method%20in.%20That%20means%20our%20code%20starting%20out%20should%20look%20something%20like%20so%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Epublic%20static%20class%20RouteMiddlewareExtensions%20%0A%7B%0A%20%20public%20static%20WebApplication%20UseExtraRoutes(this%20WebApplication%20app)%0A%20%20%7B%0A%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CH2%20id%3D%22toc-hId-599752162%22%20id%3D%22toc-hId-599752220%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23exercise-read-from-a-mock-file-and-add-support-for-raw-get-endraw-%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22exercise-read-from-a-mock-file-and-add-support-for-raw-get-endraw-%22%3E%3C%2FA%3EExercise%20-%20Read%20from%20a%20mock%20file%2C%20and%20add%20support%20for%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGET%3C%2FCODE%3E%3C%2FH2%3E%0A%3CP%3EOk%2C%20we%20know%20how%20we%20are%20starting%2C%20a%20static%20class%20and%20an%20extension%20method%2C%20so%20let's%20make%20that%20happen%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ERun%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Edotnet%20new%3C%2FCODE%3E%2C%20to%20generate%20a%20new%20minimal%20API%20project%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%20%20%20dotnet%20new%20web%20-o%20MyApi%20-f%20net6.0%0A%20%20%20cd%20Myapi%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3COL%3E%0A%3CLI%3ECreate%20a%20file%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EMockMiddleware.cs%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20give%20it%20the%20following%20code%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20using%20System.Text.Json%3B%0A%20%20%20using%20System.Text.Json.Nodes%3B%0A%0A%20%20%20public%20static%20class%20RouteMiddlewareExtensions%0A%20%20%20%7B%0A%20%20%20%20%20%20public%20static%20WebApplication%20UseExtraRoutes(this%20WebApplication%20app)%0A%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%7D%0A%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3COL%3E%0A%3CLI%3EAdd%20code%20to%20read%20a%20JSON%20file%20into%20a%20JSON%20representation%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20var%20writableDoc%20%3D%20JsonNode.Parse(File.ReadAllText(%22mock.json%22))%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3ENote%20the%20choice%20of%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EJsonNode%3C%2FCODE%3E%2C%20this%20is%20so%20we%20can%20make%20the%20JSON%20doc%20writable%2C%20which%20we%20will%20need%20for%20POST%2C%20PUT%20and%20DELETE%20later%20on.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ECreate%20the%20file%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3Emock.json%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20give%20it%20the%20following%20content%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%20%20%20%7B%0A%20%20%20%20%20%20%22Products%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22Id%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%20%20%22Name%22%3A%20%22Mock%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22Id%22%3A%202%2C%0A%20%20%20%20%20%20%20%20%20%20%22Name%22%3A%20%22Second%20Mock%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%22Orders%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22Id%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%20%20%22Name%22%3A%20%22Order1%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22Id%22%3A%202%2C%0A%20%20%20%20%20%20%20%20%20%20%22Name%22%3A%20%22Second%20Order%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%5D%0A%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CH3%20id%3D%22toc-hId-1290313636%22%20id%3D%22toc-hId-1290313694%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23add-raw-get-endraw-%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22add-raw-get-endraw-%22%3E%3C%2FA%3EAdd%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGET%3C%2FCODE%3E%3C%2FH3%3E%0A%3CP%3ELet's%20support%20our%20first%20HTTP%20verb%2C%20GET.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EAdd%20the%20following%20code%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20foreach(var%20elem%20in%20writableDoc%3F.Root.AsObject().AsEnumerable())%20%7B%0A%20%20%20%20%20%20var%20arr%20%3D%20elem.Value.AsArray()%3B%0A%20%20%20%20%20%20app.MapGet(string.Format(%22%2F%7B0%7D%22%2C%20elem.Key)%2C%20()%20%3D%26gt%3B%20elem.Value.ToString())%3B%0A%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EIn%20the%20above%20code%2C%20we%20navigate%20into%20the%20root%20object.%20Then%2C%20we%20convert%20it%20to%20an%20object%20representation%20and%20starts%20iterating%20over%20the%20keys%2C%20according%20to%20the%20mock%20file%2C%20that%20means%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EProducts%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EOrders%3C%2FCODE%3E.%20Lastly%2C%20we%20setup%20the%20route%20and%20the%20callback%2C%20the%20route%20is%20at%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eelem.Key%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20the%20value%20we%20want%20to%20return%20is%20at%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eelem.Value%3C%2FCODE%3E.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EIn%20the%20file%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EProgram.cs%3C%2FEM%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eadd%20the%20following%20line%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20app.UseExtraRoutes()%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EThe%20preceding%20code%20will%20ensure%20our%20routes%20are%20added%20to%20the%20app.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ERun%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Edotnet%20run%3C%2FCODE%3E%2C%20to%20run%20the%20app%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%20%20%20dotnet%20run%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3COL%3E%0A%3CLI%3ENavigate%20to%20the%20port%20indicated%20in%20the%20console%20output%20and%20navigate%20to%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3E%2Fproducts%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3E%2Forders%3C%2FCODE%3E%2C%20they%20should%20both%20show%20an%20output%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH3%20id%3D%22toc-hId--517140827%22%20id%3D%22toc-hId--517140769%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23add-raw-get-endraw-by-id%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22add-raw-get-endraw-by-id%22%3E%3C%2FA%3EAdd%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EGET%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eby%20id%3C%2FH3%3E%0A%3CP%3EOk%2C%20you%20got%20the%20basic%20GET%20case%20to%20work%2C%20what%20about%20filtering%20the%20data%20with%20parameter.%20Using%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3E%2Fproducts%2F1%3C%2FCODE%3E%2C%20should%20just%20return%20one%20record%20back.%20How%20do%20we%20do%20that%3F%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EAdd%20the%20following%20code%20in%20the%20foreach%20loop%20in%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EMockMiddlware.cs%3C%2FEM%3E%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eapp.MapGet(string.Format(%22%2F%7B0%7D%22%2C%20elem.Key)%20%2B%20%22%2F%7Bid%7D%22%2C%20(int%20id)%20%3D%26gt%3B%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20matchedItem%20%3D%20arr.SingleOrDefault(row%20%3D%26gt%3B%20row%0A%20%20%20%20%20%20%20%20%20%20.AsObject()%0A%20%20%20%20%20%20%20%20%20%20.Any(o%20%3D%26gt%3B%20o.Key.ToLower()%20%3D%3D%20%22id%22%20%26amp%3B%26amp%3B%20int.Parse(o.Value.ToString())%20%3D%3D%20id)%0A%20%20%20%20%20%20%20%20)%3B%0A%20%20%20%20%20%20%20%20return%20matchedItem%3B%0A%20%20%20%20%20%20%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EThe%20above%20code%20is%20iterating%20over%20the%20rows%20for%20a%20specific%20route%20and%20looks%20for%20an%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eid%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproperty%20that%20matches%20our%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3E%7Bid%7D%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Epattern.%20The%20found%20item%20is%20returned.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3ERun%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Edotnet%20run%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eto%20test%20out%20this%20code%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%20%20dotnet%20run%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3COL%3E%0A%3CLI%3ENavigate%20to%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3E%2Fproducts%2F1%3C%2FCODE%3E%2C%20you%20should%20see%20the%20following%20JSON%20output%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3E%20%20%20%7B%0A%0A%20%20%20%20%20%20%22Id%22%3A%201%2C%0A%20%20%20%20%20%20%22Name%22%3A%20%22Mock%22%0A%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EGreat%2C%20we%20got%20it%20to%20work.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1146125808%22%20id%3D%22toc-hId-1146125866%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--661328655%22%20id%3D%22toc-hId--661328597%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23exercise-write-data%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22exercise-write-data%22%3E%3C%2FA%3EExercise%20-%20write%20data%3C%2FH2%3E%0A%3CP%3ENow%20that%20we%20can%20read%20data%20from%20our%20mock%20API%2C%20lets%20tackle%20writing%20data.%20The%20fact%20that%20we%20were%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EJsonNode.Parse()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ein%20the%20beginning%20makes%20it%20possible%20for%20us%20to%20use%20operations%20on%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EJsonNode%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Einstance.%20In%20short%2C%20our%20approach%20will%20be%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3Efind%20the%20specific%20place%20in%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EJsonNode%3C%2FCODE%3E%2C%20that%20represents%20our%20mock%20data%2C%20and%20change%20it%3C%2FLI%3E%0A%3CLI%3Esave%20down%20the%20whole%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EJsonNode%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Einstance%20to%20our%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3Emock.json%3C%2FEM%3E.%20If%20the%20user%20uses%20an%20operation%20to%20change%20the%20data%2C%20that%20should%20be%20reflected%20in%20the%20Mock%20file.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH3%20id%3D%22toc-hId-1955266897%22%20id%3D%22toc-hId-1955266955%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23add-raw-post-endraw-%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22add-raw-post-endraw-%22%3E%3C%2FA%3EAdd%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EPOST%3C%2FCODE%3E%3C%2FH3%3E%0A%3CP%3ETo%20implement%20this%20route%2C%20we%20will%20use%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EMapPost()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ebut%20we%20can't%20just%20give%20it%20a%20typed%20object%20in%20the%20callback%20for%20the%20route%2C%20because%20we%20don't%20know%20what%20it%20looks%20like.%20Instead%2C%20we%20will%20use%20the%20request%20object%2C%20read%20the%20body%20and%20add%20that%20to%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EJsonNode%3C%2FCODE%3E.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EAdd%20following%20code%20to%20support%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EPOST%3C%2FCODE%3E%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20app.MapPost(string.Format(%22%2F%7B0%7D%22%2C%20elem.Key)%2C%20async%20(HttpRequest%20request)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20string%20content%20%3D%20string.Empty%3B%0A%20%20%20%20%20%20%20%20using(StreamReader%20reader%20%3D%20new%20StreamReader(request.Body))%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20content%20%3D%20await%20reader.ReadToEndAsync()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20var%20newNode%20%3D%20JsonNode.Parse(content)%3B%0A%20%20%20%20%20%20%20%20var%20array%20%3D%20elem.Value.AsArray()%3B%0A%20%20%20%20%20%20%20%20newNode.AsObject().Add(%22Id%22%2C%20array.Count()%20%2B%201)%3B%0A%20%20%20%20%20%20%20%20array.Add(newNode)%3B%0A%0A%20%20%20%20%20%20%20%20File.WriteAllText(%22mock.json%22%2C%20writableDoc.ToString())%3B%0A%20%20%20%20%20%20%20%20return%20content%3B%0A%20%20%20%20%20%20%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EIn%20the%20above%20code%2C%20we%20have%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Erequest%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eas%20input%20parameter%20to%20our%20route%20handler%20function.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20app.MapPost(string.Format(%22%2F%7B0%7D%22%2C%20elem.Key)%2C%20async%20(HttpRequest%20request)%20%3D%26gt%3B%20%7B%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EThen%20we%20read%20the%20body%2C%20using%20a%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EStreamReader%3C%2FCODE%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20using(StreamReader%20reader%20%3D%20new%20StreamReader(request.Body))%0A%20%7B%0A%20%20%20content%20%3D%20await%20reader.ReadToEndAsync()%3B%0A%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3ENext%2C%20we%20construct%20a%20JSON%20representation%20from%20our%20received%20BODY%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evar%20newNode%20%3D%20JsonNode.Parse(content)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EThis%20is%20followed%20by%20locating%20the%20place%20to%20insert%20this%20new%20JSON%20and%20adding%20it%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evar%20array%20%3D%20elem.Value.AsArray()%3B%0AnewNode.AsObject().Add(%22Id%22%2C%20array.Count()%20%2B%201)%3B%0Aarray.Add(newNode)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3ELastly%2C%20we%20update%20the%20mock%20file%20and%20respond%20something%20back%20to%20the%20calling%20client%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EFile.WriteAllText(%22mock.json%22%2C%20writableDoc.ToString())%3B%0Areturn%20content%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CH3%20id%3D%22toc-hId-147812434%22%20id%3D%22toc-hId-147812492%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23add-raw-delete-endraw-%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22add-raw-delete-endraw-%22%3E%3C%2FA%3EAdd%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EDELETE%3C%2FCODE%3E%3C%2FH3%3E%0A%3CP%3ETo%20support%20deletion%2C%20we%20need%20a%20very%20similar%20approach%20to%20how%20we%20located%20an%20entry%20by%20id%20parameter.%20We%20also%20need%20to%20locate%20where%20to%20delete%20in%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EJsonObject%3C%2FCODE%3E.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EAdd%20the%20following%20code%20to%20support%20delete%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20app.MapDelete(string.Format(%22%2F%7B0%7D%22%2C%20elem.Key)%20%2B%20%22%2F%7Bid%7D%22%2C%20(int%20id)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20var%20matchedItem%20%3D%20arr%0A%20%20%20%20%20%20%20%20%20.Select((value%2C%20index)%20%3D%26gt%3B%20new%7B%20value%2C%20index%7D)%0A%20%20%20%20%20%20%20%20%20.SingleOrDefault(row%20%3D%26gt%3B%20row.value%0A%20%20%20%20%20%20%20%20%20%20.AsObject()%0A%20%20%20%20%20%20%20%20%20%20.Any(o%20%3D%26gt%3B%20o.Key.ToLower()%20%3D%3D%20%22id%22%20%26amp%3B%26amp%3B%20int.Parse(o.Value.ToString())%20%3D%3D%20id)%0A%20%20%20%20%20%20%20%20)%3B%0A%20%20%20%20%20%20%20%20if%20(matchedItem%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20arr.RemoveAt(matchedItem.index)%3B%0A%20%20%20%20%20%20%20%20%20%20File.WriteAllText(%22mock.json%22%2C%20writableDoc.ToString())%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20return%20%22OK%22%3B%0A%20%20%20%20%20%20%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EFirst%2C%20we%20find%20the%20item%20in%20question%2C%20but%20we%20also%20make%20sure%20that%20we%20know%20what%20the%20index%20of%20the%20found%20item%20is.%20We%20will%20use%20this%20index%20later%20to%20remove%20the%20item.%20Hence%2C%20we%20get%20the%20following%20code%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20var%20matchedItem%20%3D%20arr%0A%20%20%20%20%20%20%20%20%20.Select((value%2C%20index)%20%3D%26gt%3B%20new%7B%20value%2C%20index%7D)%0A%20%20%20%20%20%20%20%20%20.SingleOrDefault(row%20%3D%26gt%3B%20row.value%0A%20%20%20%20%20%20%20%20%20%20.AsObject()%0A%20%20%20%20%20%20%20%20%20%20.Any(o%20%3D%26gt%3B%20o.Key.ToLower()%20%3D%3D%20%22id%22%20%26amp%3B%26amp%3B%20int.Parse(o.Value.ToString())%20%3D%3D%20id)%0A%20%20%20%20%20%20%20%20)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EOur%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EmatchedItem%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Enow%20contains%20either%20NULL%20or%20an%20object%20that%20has%20an%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eindex%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproperty.%20Using%20this%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Eindex%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproperty%2C%20we%20will%20be%20able%20to%20perform%20deletions%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20if%20(matchedItem%20!%3D%20null)%20%7B%0A%20%20%20%20%20arr.RemoveAt(matchedItem.index)%3B%0A%20%20%20%20%20File.WriteAllText(%22mock.json%22%2C%20writableDoc.ToString())%3B%0A%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3ETo%20test%20writes%2C%20use%20something%20like%20Postman%20or%20Advanced%20REST%20client%2C%20it%20should%20work.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1659642029%22%20id%3D%22toc-hId--1659641971%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23add-route-info%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22add-route-info%22%3E%3C%2FA%3EAdd%20route%20info%3C%2FH3%3E%0A%3CP%3EWe're%20almost%20done%2C%20as%20courtesy%20towards%20the%20programmer%20using%20this%20code%2C%20we%20want%20to%20print%20out%20what%20routes%20we%20have%20and%20support%20so%20it's%20easy%20to%20know%20what%20we%20support.%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EAdd%20this%20code%2C%20just%20at%20the%20start%20of%20the%20method%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3EUseExtraRoutes()%3C%2FCODE%3E%3A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3E%20%20%20%2F%2F%20print%20API%0A%20%20%20%20foreach%20(var%20elem%20in%20writableDoc%3F.Root.AsObject().AsEnumerable())%7B%0A%20%20%20%20%20%20Console.WriteLine(string.Format(%22GET%20%2F%7B0%7D%22%2C%20elem.Key.ToLower()))%3B%0A%20%20%20%20%20%20Console.WriteLine(string.Format(%22GET%20%2F%7B0%7D%22%2C%20elem.Key.ToLower())%20%2B%20%22%2Fid%22)%3B%0A%20%20%20%20%20%20Console.WriteLine(string.Format(%22POST%20%2F%7B0%7D%22%2C%20elem.Key.ToLower()))%3B%0A%20%20%20%20%20%20Console.WriteLine(string.Format(%22DELETE%20%2F%7B0%7D%22%2C%20elem.Key.ToLower())%20%2B%20%22%2Fid%22)%3B%0A%20%20%20%20%20%20Console.WriteLine(%22%20%22)%3B%0A%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EThat's%20it%2C%20that's%20all%20we%20intend%20to%20implement.%20Hopefully%2C%20this%20is%20all%20useful%20to%20you%20and%20you%20will%20be%20able%20to%20use%20it%20next%20you%20just%20want%20an%20API%20up%20and%20running%20that%20you%20can%20build%20a%20front-end%20app%20off%20of.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-827870804%22%20id%3D%22toc-hId-827870862%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23full-code%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22full-code%22%3E%3C%2FA%3EFull%20code%3C%2FH3%3E%0A%3CP%3EIf%20you%20got%20lost%20at%20any%20point%2C%20here's%20the%20full%20code%3A%3C%2FP%3E%0A%3CP%3E%3CEM%3EProgram.cs%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20Mock%3B%0Avar%20builder%20%3D%20WebApplication.CreateBuilder(args)%3B%0Avar%20app%20%3D%20builder.Build()%3B%0A%0Aapp.MapGet(%22%2F%22%2C%20()%20%3D%26gt%3B%20%22Hello%20World!%22)%3B%0Aapp.UseExtraRoutes()%3B%20%2F%2F%20this%20is%20where%20our%20routes%20get%20added%0A%0Aapp.Run()%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%3CEM%3EMockMiddleware.cs%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Eusing%20System.Text.Json%3B%0Ausing%20System.Text.Json.Nodes%3B%0A%0Anamespace%20Mock%3B%0A%0Apublic%20static%20class%20RouteMiddlewareExtensions%20%0A%7B%0A%20%20public%20static%20WebApplication%20UseExtraRoutes(this%20WebApplication%20app)%0A%20%20%7B%0A%20%20%20%20var%20writableDoc%20%3D%20JsonNode.Parse(File.ReadAllText(%22mock.json%22))%3B%0A%0A%20%20%20%20%2F%2F%20print%20API%0A%20%20%20%20foreach%20(var%20elem%20in%20writableDoc%3F.Root.AsObject().AsEnumerable())%7B%0A%20%20%20%20%20%20Console.WriteLine(string.Format(%22GET%20%2F%7B0%7D%22%2C%20elem.Key.ToLower()))%3B%0A%20%20%20%20%20%20Console.WriteLine(string.Format(%22GET%20%2F%7B0%7D%22%2C%20elem.Key.ToLower())%20%2B%20%22%2Fid%22)%3B%0A%20%20%20%20%20%20Console.WriteLine(string.Format(%22POST%20%2F%7B0%7D%22%2C%20elem.Key.ToLower()))%3B%0A%20%20%20%20%20%20Console.WriteLine(string.Format(%22DELETE%20%2F%7B0%7D%22%2C%20elem.Key.ToLower())%20%2B%20%22%2Fid%22)%3B%0A%20%20%20%20%20%20Console.WriteLine(%22%20%22)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2F%20setup%20routes%0A%20%20%20%20foreach(var%20elem%20in%20writableDoc%3F.Root.AsObject().AsEnumerable())%20%7B%0A%20%20%20%20%20%20var%20arr%20%3D%20elem.Value.AsArray()%3B%0A%20%20%20%20%20%20app.MapGet(string.Format(%22%2F%7B0%7D%22%2C%20elem.Key)%2C%20()%20%3D%26gt%3B%20elem.Value.ToString())%3B%0A%20%20%20%20%20%20app.MapGet(string.Format(%22%2F%7B0%7D%22%2C%20elem.Key)%20%2B%20%22%2F%7Bid%7D%22%2C%20(int%20id)%20%3D%26gt%3B%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20var%20matchedItem%20%3D%20arr.SingleOrDefault(row%20%3D%26gt%3B%20row%0A%20%20%20%20%20%20%20%20%20%20.AsObject()%0A%20%20%20%20%20%20%20%20%20%20.Any(o%20%3D%26gt%3B%20o.Key.ToLower()%20%3D%3D%20%22id%22%20%26amp%3B%26amp%3B%20int.Parse(o.Value.ToString())%20%3D%3D%20id)%0A%20%20%20%20%20%20%20%20)%3B%0A%20%20%20%20%20%20%20%20return%20matchedItem%3B%0A%20%20%20%20%20%20%7D)%3B%20%20%20%20%20%20%0A%20%20%20%20%20%20app.MapPost(string.Format(%22%2F%7B0%7D%22%2C%20elem.Key)%2C%20async%20(HttpRequest%20request)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20string%20content%20%3D%20string.Empty%3B%0A%20%20%20%20%20%20%20%20using(StreamReader%20reader%20%3D%20new%20StreamReader(request.Body))%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20content%20%3D%20await%20reader.ReadToEndAsync()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20var%20newNode%20%3D%20JsonNode.Parse(content)%3B%0A%20%20%20%20%20%20%20%20var%20array%20%3D%20elem.Value.AsArray()%3B%0A%20%20%20%20%20%20%20%20newNode.AsObject().Add(%22Id%22%2C%20array.Count()%20%2B%201)%3B%0A%20%20%20%20%20%20%20%20array.Add(newNode)%3B%0A%0A%20%20%20%20%20%20%20%20File.WriteAllText(%22mock.json%22%2C%20writableDoc.ToString())%3B%0A%20%20%20%20%20%20%20%20return%20content%3B%0A%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20app.MapPut(string.Format(%22%2F%7B0%7D%22%2C%20elem.Key)%2C%20()%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20return%20%22TODO%22%3B%0A%20%20%20%20%20%20%7D)%3B%0A%20%20%20%20%20%20app.MapDelete(string.Format(%22%2F%7B0%7D%22%2C%20elem.Key)%20%2B%20%22%2F%7Bid%7D%22%2C%20(int%20id)%20%3D%26gt%3B%20%7B%0A%0A%20%20%20%20%20%20%20%20var%20matchedItem%20%3D%20arr%0A%20%20%20%20%20%20%20%20%20.Select((value%2C%20index)%20%3D%26gt%3B%20new%7B%20value%2C%20index%7D)%0A%20%20%20%20%20%20%20%20%20.SingleOrDefault(row%20%3D%26gt%3B%20row.value%0A%20%20%20%20%20%20%20%20%20%20.AsObject()%0A%20%20%20%20%20%20%20%20%20%20.Any(o%20%3D%26gt%3B%20o.Key.ToLower()%20%3D%3D%20%22id%22%20%26amp%3B%26amp%3B%20int.Parse(o.Value.ToString())%20%3D%3D%20id)%0A%20%20%20%20%20%20%20%20)%3B%0A%20%20%20%20%20%20%20%20if%20(matchedItem%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20arr.RemoveAt(matchedItem.index)%3B%0A%20%20%20%20%20%20%20%20%20%20File.WriteAllText(%22mock.json%22%2C%20writableDoc.ToString())%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20return%20%22OK%22%3B%0A%20%20%20%20%20%20%7D)%3B%0A%0A%20%20%20%20%7D%3B%0A%0A%20%20%20%20return%20app%3B%0A%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%20js-code-highlight%22%3E%0A%3CDIV%20class%3D%22highlight__panel%20js-actions-panel%22%3E%0A%3CDIV%20class%3D%22highlight__panel-action%20js-fullscreen-code-action%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CH3%20id%3D%22toc-hId--979583659%22%20id%3D%22toc-hId--979583601%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23update-homework%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22update-homework%22%3E%3C%2FA%3EUpdate%20-%20homework%3C%2FH3%3E%0A%3CP%3EFor%20your%20homework%2C%20see%20if%20you%20can%20implement%20PUT.%20%3A)%3C%2Fimg%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1378846455%22%20id%3D%22toc-hId-1378846513%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId--428608008%22%20id%3D%22toc-hId--428607950%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fdotnet%2Fhow-you-can-build-a-mock-api-based-on-json-for-asp-net-and-minimal-api-1dmd%23summary%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22summary%22%3E%3C%2FA%3ESummary%3C%2FH2%3E%0A%3CP%3EI%20took%20you%20through%20a%20journey%20of%20implementing%20a%20Mock%20API%20for%20minimal%20APIs.%20Hopefully%2C%20you%20found%20this%20useful%20and%20can%20use%20it%20in%20a%20future%20project.%3C%2FP%3E%0A%3CP%3EHere's%20a%20repo%20for%20the%20code%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fsoftchris%2Fmock-sharp.git%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Erepo%3C%2FA%3E%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3EPlease%20comment%20if%20you%20want%20me%20to%20make%20this%20into%20either%20a%20tool%20or%20a%20NuGet%20package%20%3A)%3C%2Fimg%3E%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-3196039%22%20slang%3D%22en-US%22%3E%3CP%3ETLDR%3B%20this%20article%20describes%20how%20to%20create%20a%20Mock%20API%20from%20a%20JSON%20file%20for%20minimal%20API%20in%20ASP%20.NET%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pexels-magda-ehlers-9891049.jpg%22%20style%3D%22width%3A%20800px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F350143i566F01C8DC871C86%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22pexels-magda-ehlers-9891049.jpg%22%20alt%3D%22pexels-magda-ehlers-9891049.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3196039%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Feb 21 2022 05:06 PM
Updated by: