ASP.Net MVC: how to create a JsonResult based on raw Json Data - asp.net-mvc

Having a string containing the following raw Json data (simplified for the sake of the question):
var MyString = "{ 'val': 'apple' }";
How can I create a JsonResult object representing MyString?
I tried to use the Json(object) method. but it handles the raw json data as an string -logically :P-. So the returned HTTP response looks like:
"{ 'val': 'apple' }"
instead of the given raw Json Data:
{ 'val': 'apple' }
this is what I want to achieve:

The Json() method on Controller is actually a helper method that creates a new JsonResult. If we look at the source code for this class*, we can see that it's not really doing that much -- just setting the content type to application/json, serializing your data object using a JavaScriptSerializer, and writing it the resulting string.. You can duplicate this behavior (minus the serialization, since you've already done that) by returning a ContentResult from your controller instead.
public ActionResult JsonData(int id) {
var jsonStringFromSomewhere = "{ 'val': 'apple' }";
// Content() creates a ContentResult just as Json() creates a JsonResult
return Content(jsonStringFromSomewhere, "application/json");
}
* Starting in MVC2, JsonResult also throws an exception if the user is making an HTTP GET request (as opposed to say a POST). Allowing users to retrieve JSON using an HTTP GET has security implications which you should be aware of before you permit this in your own app.

The way I have generated json data from a string is by using JavaScriptResult in the controller:
public JavaScriptResult jsonList( string jsonString)
{
jsonString = "var jsonobject = new Array(" + jsonString + ");";
return JavaScript(jsonString)
}
Then when you request pass the json string to that action in your controller, the result will be a file with javascript headers.

I think you can use the JavaScriptSerializer class for this
var js = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObject = js.Deserialize("{ 'val': 'apple' }", typeof(object));

Related

How to return view along with model in Async task in Asp.Net MVC

I am testing asynch task in MVC and creating asynchronous task following code. When I return model value along with view name return View("Index", EmpResponse), I am getting error. but if I simply return view return view(). it is working well.
public class AsynchController : Controller
{
string Baseurl = "http://dummy.restapiexample.com/api/v1/";
// GET: Asynch
public async Task<ActionResult> Index()
{
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync("employees");
var EmpResponse = "";
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
}
//returning the employee list to view
return View("Index", EmpResponse);
}
}
In simply ActionResult, I know we can return view name and model both together. is it issue with Async task?
Your intention is to pass EmpResponse as the view-model for your Index view, but since you have already deserialized EmpResponse as a string, it matches the wrong overload of the View() helper method (the one which accepts both viewName and masterName).
Try to pass it as an object to match the correct overload:
return View("Index", EmpResponse as object);
A better approach would be to store the received data as a strongly-typed collection of objects:
var empResponse = await Res.Content.ReadAsAsync<IEnumerable<Employee>>();
Then pass it as a view-model:
return View("Index", empResponse);
This isn't really an async issue, but a model type issue. (Though there is an async issue waiting to become a problem... Don't call .Result directly, but instead use await to get the result.)
Your model is a string. But the overload for View() which takes a second string uses it to find a named view. Which is why it's looking for a view called your long JSON string. (Well, a "master view" in this case since you're sending it two strings.)
Don't use a string as a model. Use a model. Instead of sending one big JSON string to your view, deserialize it into a model of some sort. The type is up to you, but the deserialization might look something like:
var response = await client.GetAsync("employees");
YourModelTypeHere model = null;
if (response.IsSuccessStatusCode)
{
var responseString = await result.Content.ReadAsStringAsync();
model = JsonConvert.DeserializeObject<YourModelTypeHere>(responseString);
}
return View(model);
There may even be an option in result to read/deserialize as your model directly, saving you a line of code above. But the overall principle is the same. Use strongly typed models instead of complex serialized strings.
*In this case, YourModelTypeHere looks like it would in fact be an IEnumerable<YourModel> or perhaps an IList<YourModel>, based on the serialized JSON we're seeing.
*Note also that this uses your current logic of sending an empty model to the same view if nothing was successfully retrieved. For an empty string that may be okay, for null it may become problematic depending on what your view is doing. (Either way your view is going to have to change if it currently expects a string as a model.) Perhaps redirect or return an error in the case of no available model? The logic of how your system should behave is up to you.

How do I handle untyped POST data in ASP.NET MVC?

Something is going to call my web server with the url http://testserver.fake.com/responses/123 with a POST.
What I want to do is save the body of the POST to a file such as "\fileserver\response\123.response".
What should the signature look like on the controller?
Here you go:
public ActionResult SavePostAction(int responseId)
{
// read post data straight from the request.
string postData = new StreamReader(HttpContext.Request.InputStream).ReadToEnd();
// create a json file
string json = JsonConvert.SerializeObject(new
{
postData = postData
});
// Save the file to your filesystem\db etc..
System.IO.File.WriteAllText(#"c:\temp\" + responseId + ".reponse", json);
return new EmptyResult(); // or whatever
}

How can I post JSON to an ASP.NET controller action?

How can I post JSON to an ASP.NET MVC controller action?
sample scenario
An external authentication system requires JSON be posted to it. I would like to capture the user's credentials on my site and forward them as JSON to the authentication site. Redirects are not an option.
So we'll use http://www.example.com/ExtAuth/Login as our fictitious external authentication endpoint.
ExtAuth will expect us to post a JSON string representing an object with two properties: User, and Password.
ExtAuth will return a JSON string representing an object with two properties: Status, and Message.
The key to making this whole thing work is the extRequest.ContentType. It MUST be set to application/json.
I'll leave proper error handling as an exercise to the user.
<HttpPost>
<AllowAnonymous>
Public Function Login(ByVal model As LoginModel) As ActionResult
Dim authEndpointUrl As String = "http://www.example.com/ExtAuth/Login"
Dim result As String = String.Empty ' this will hold the JSON returned from ExtAuth
Dim resultModel As LoginResult = Nothing ' The deserialized form of the result JSON
Dim data As String = String.Empty ' the serialized representation of our login data
Dim extRequest As HttpWebRequest = WebRequest.CreateHttp(authEndpointUrl)
extRequest.Method = "POST"
extRequest.ContentType = "application/json"
data = Newtonsoft.Json.JsonConvert.SerializeObject(model)
Using writer As StreamWriter = New StreamWriter(extRequest.GetRequestStream)
writer.Write(data)
End Using
Using extResponse As HttpWebResponse = extRequest.GetResponse
Using reader As StreamReader = New StreamReader(extResponse.GetResponseStream)
result = reader.ReadToEnd
End Using
End Using
resultModel = Newtonsoft.Json.JsonConvert.DeserializeObject(Of LoginResult)(result)
ViewData("Status") = resultModel.Status
ViewData("Message") = resultModel.Message
Return View(model)
End Function
This method should work for MVC 3+.

Why does the Breeze Web API implementation respond to metadata requests with a string instead of a JSON object?

Is there any reason that the Breeze Web API implementation of the response to any metadata requests returns a string instead of a JSON object?
Sending metadata as text adds a lot of overhead over the network (due " encoding) and on clientside due manual JSON.parse.
I think that your controller can simply return the Metadata as JSON by specifying the contentType header:
i.e.
[HttpGet]
public HttpResponseMessage Metadata()
{
var result = new HttpResponseMessage { Content = new StringContent(_contextProvider.Metadata())};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return result;
}
As of v 1.2.7, the BreezeController attribute now does this automatically.... and thanks for the idea.

How to convert form values into json format

I am using Form. In my database data is stored in the form{type:"input",name:"name",Label:"Login"} I am using ajax function to get these values.
I want to convert this into json object format, How can I convert into json object format ?
If you are asking, how to get value from an action using an ajax function in a Json object fomat, below is the sample code for that.
Here I am making an ajax call to "GetData" action.
var url = '#Url.Action("GetData")';
$.ajax({
url: url,
type: 'GET',
cache: false,
data: { value: strId},
success: function (result) {
// do what you want with result returned here in JSON format
}
});
Update: In the above success method, you can read JSON values as result.type, result.name and result.Label.
and here is the Action, which is returning JSON data
public ActionResult GetData(string id)
{
return Json(new {type="type", name="name", Label="Login"});
}
From OP's comment,
Is it possible to create json object in the controller, If possible
how to create it?
You can try JSON.NET to create JSON objects easily in an MVC application.
For ex. you could do something like this in JSON.NET,
dynamic jsonObj = new JObject();
jsonObj.Name = "Mark";
jsonObj.Age = 29;
var jsonString = jsonObj.ToString(); // { "Name" : "Mark", "Age" : 29 }
You can also create json object from parsing a string,
dynamic jsonObj = JObject.Parse(jsonString);
You can download JSON.NET from here.

Resources