user584018
user584018

Reputation: 11304

not getting json content

I have a json string content,

[{
"id": 1,
"name": "Fort Craig Elementary",
"principal": "Michelle Thorne"
}]

and I'm writing very simple asp.net Web API method to get above json,

[Route("api/schools")]
    public HttpResponseMessage Get()
    {
        var json = HttpContext.Current.Server.MapPath("~/data/schools.json");
        var response = Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent(json, Encoding.UTF8, "application/json");
        return response;
    }

while I m hitting URL "http://localhost:7602/api/schools", it's give me path of json instead json content,

c:\users\ajmal\documents\visual studio 2013\projects\Route\Route\data\schools.json

Please suggest how to get json content.

Thanks,

Upvotes: 0

Views: 135

Answers (1)

DonO
DonO

Reputation: 1070

Its doing exactly what you have put in the code. Import System.IO and use the stream reader to read the file. Your var json should actually be var path to make more sense.

 using (var sr = new StreamReader(path)) 
            var json = sr.ReadToEnd();

Upvotes: 1

Related Questions