0

I'm trying to read this Json file (types.json):

{
  "types": [
    "serveur d'application",
    "serveur de donnees",
    "serveur de proxy"
  ]
}

with the following function in my controller:

[HttpGet("/types")]
public async Task<ActionResult<Types>> GetInstancesTypes()
{
    Types typesGet; 
    using (StreamReader r = new StreamReader("../types.json"))
    {
        string json = r.ReadToEnd();
        typesGet = JsonConvert.DeserializeObject<Types>(json);
    }

    return typesGet;
}

The class Types is:

public class Types
    {
        public IList<string> types { get; set; }
    }

Here is my project structure

When I go to the link where I should see the result it gives me this error:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|b6dd40c-4159a247507490ac.",
    "errors": {
        "id": [
            "The value 'types' is not valid."
        ]
    }
}

Any help would be appreciated thank you :)

Hasagiii
  • 345
  • 1
  • 3
  • 21

1 Answers1

2

Also, if anyone has an idea on how can I read data from a Json file and show it on the browser ?

I tried your json module and it works fine, as stated in one of the comment, its a routing problem

Controller

public async Task<ActionResult<Types>> Index()
{
var path = @"C:\Users\John\URLShortnet\CoreApp1\types.json";
Types typesGet;
using (StreamReader r = new StreamReader(path))
{
string json = r.ReadToEnd();
typesGet = JsonConvert.DeserializeObject<Types>(json);
}


// return typesGet;

return View(typesGet);
}

View (Index.cshtml)


@foreach (var item in Model.types)
{
<p>@item</p>
}

enter image description here

Clint
  • 6,011
  • 1
  • 21
  • 28