Reputation: 814
We are trying to make mock service to serve JSON. We have plain JSON strings stored in static files and want to serve them to client as they are, without any additional wrappers.
E.g. we have json string {"result_code":200,{"name":"John", "lastName": "Doe"}}
and we want to get json response on client just like this without any Content or Data wrappers.
We have solution where we use data contracts and deserialize json to C# objects, but that's a bit complicated and we don't need it.
Thank you
Upvotes: 20
Views: 51034
Reputation: 1479
You can do this by referencing System.Web.Mvc. Example in a quick console app I threw together:
using System;
using System.Web.Mvc;
using Newtonsoft.Json;
namespace Sandbox
{
class Program
{
private static void Main(string[] args)
{
//Added "person" to the JSON so it would deserialize
var testData = "{\"result_code\":200, \"person\":{\"name\":\"John\", \"lastName\": \"Doe\"}}";
var result = new JsonResult
{
Data = JsonConvert.DeserializeObject(testData)
};
Console.WriteLine(result.Data);
Console.ReadKey();
}
}
}
You can just return the JsonResult from the mock method.
Upvotes: 21
Reputation: 1944
You can return a static JSON string by sending the content manually.
public ActionResult Tester()
{
return Content("{\"result_code\":200,{\"name\":\"John\", \"lastName\": \"Doe\"}}", "application/json");
}
Sorry if that's not exactly what you're asking about
Upvotes: 22