How to show html embeded inside JSON to the user with AngularJS and ngSanitize.

 

image

 

I want to show HTML to the user, that was stored in a SQL Server Database. The data was queried from the SQL Server Database by using Entity Framework, then the C# model was converted to JSON, by using JSON.net and send to the browser by the ASP .NET web api service.

To show this HTML to the user with AngularJS, I used ngSanitize and the ng-bind-html directive:

(at this moment plunker is down, so I will edit this post in the future to add a plunker).

 

If the HTML contains angular bindings / directives, you can use the compile service to bind the scope to the html fragment:

https://docs.angularjs.org/api/ng/service/$compile

So instead of using the ng-bind-html, you can use this directive:

https://github.com/incuna/angular-bind-html-compile/blob/master/angular-bind-html-compile.js

<!DOCTYPE html>
<html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="spike">
<head>
<!-- Why is angular added to the html tag? Because IE8 won't work without it. -->
<title>Spike page</title>
<meta charset="utf-8">
<!-- Why is "edge" used? Because we want the highest support for modern standards. -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- Library scripts -->
<!--<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular-animate.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular-sanitize.min.js"></script>
<!-- Live reload script with grunt. -->
<!--<script src="http://localhost:35729/livereload.js"></script>-->
<!-- App styles -->
<style>
/*
A small custom reset stylesheet is used, to fix style differences between browsers.
*/
html, body
{
height: 100%;
}
body, div, p, ul, li, button
{
border: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
font-family: "Open Sans", sans-serif;
margin: 0;
outline: 0;
padding: 0;
}
body
{
background-color: #F1F1F1;
padding: 20px;
}
.spike-title
{
color: #393939;
}
.spike-content
{
color: #393939;
}
.spike-content-with-class
{
color: #ff0000;
}
</style>
<!-- App scripts -->
<script>
(function ()
{
"use strict";
var app = angular.module("spike", ["ngAnimate", "ngSanitize"]);
}());
(function ()
{
"use strict";
var app = angular.module("spike");
var controller = function ($scope)
{
$scope.title = "How to show html embeded inside JSON to the user with AngularJS and ngSanitize.";
var json = '{ \"html\":\"<br><br><p>Some html in JSON.</p><br><br><p class=\\"spike-content-with-class\\">Some html in JSON with a class attribute.</p>\" }';
var content = JSON.parse(json);
$scope.content = content.html;
};
app.controller("main", ["$scope", controller]);
}());
</script>
</head>
<body>
<div ng-controller="main">
<h2 class="spike-title">{{ title }}</h2>
<div class="spike-content" ng-bind-html="content"></div>
</div>
</body>
</html>

POST multiple parameters to an ASP .NET Web Api REST service from a .NET 3.5 assembly, by using one dynamic JObject "options" parameter.

 

Client code in .NET 3.5

 

namespace WordMerge.EndToEndTests
{
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using Newtonsoft.Json;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void Execute_a_post_request()
{
string url = "http://localhost:63544/api/document";
object result = string.Empty;
// Uses the System.Net.WebClient and not HttpClient, because .NET 2.0 must be supported.
using (var client = new WebClient())
{
// Set the header so it knows we are sending JSON.
client.Headers[HttpRequestHeader.ContentType] = "application/json";
// Create the one and only "options" parameter object.
var dto = new DocumentDto
{
TemplatePath = @"C:\Temp\Templates",
DestinationPath = @"C:\Temp\Destination",
Data = new string[] { "This", " is", " test", " input", " data." }
};
// Serialise the data we are sending in to JSON
string serialisedData = JsonConvert.SerializeObject(dto);
// Make the request
var response = client.UploadString(url, serialisedData);
// Deserialise the response into a GUID
result = JsonConvert.DeserializeObject(response);
}
Assert.AreEqual(@"Succesfully uploaded: This, is, test, input, data.", result.ToString());
}
}
public class DocumentDto
{
public string TemplatePath { get; set; }
public string DestinationPath { get; set; }
public string[] Data { get; set; }
}
}

Server code in .NET 4.5

 

 

namespace Service.api
{
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
public class DocumentController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
public string Post([FromBody]JObject jsonData)
{
// Convert the dynamic JObject to a DocumentDto object.
DocumentDto dto = jsonData.ToObject<DocumentDto>();
// Use the given data to create the result.
string seperator = ",";
string data = string.Join(seperator, dto.Data.ToList<string>());
string result = string.Format("Succesfully uploaded: {0}", data);
return result;
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
public class DocumentDto
{
public string TemplatePath { get; set; }
public string DestinationPath { get; set; }
public string[] Data { get; set; }
}
}

JSON Serialization and Deserialization

Found a good articale on JSON Serialization and Deserialization:

http://www.codeproject.com/Articles/272335/JSON-Serialization-and-Deserialization-in-ASP-NET

I used this article to create a class that serializes DataSet, DataTable and DataRow objects:

I just changed one aspect of the codeproject article, I used the regular expression "\\/Date\((-?\d+)\)\\/" instead of  “\\/Date\((\d+)\+\d+\)\\/”. The regular expression “\\/Date\((-?\d+)\)\\/” takes into account dates before epoch (1979-1-1).

/// <summary>
/// Contains JSON helper functions.
/// </summary>
public class JsonHelper
{
/// <summary>
/// Date time format, used to convert a datetime to a string.
/// By default "yyyy-MM-dd HH:mm:ss".
/// </summary>
public string DateTimeFormat { get; set; }
/// <summary>
/// Initializes properties.
/// </summary>
public JsonHelper()
{
DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
}
/// <summary>
/// Converts a DataSet to a JSON string.
/// </summary>
/// <param name="dataSet">The dataset to convert.</param>
/// <exception cref="System.ArgumentException">Thrown when parameter [dataSet] is null.</exception>
/// <returns>
/// - JSON in the format [[[Table0Row0Column0,Table0Row0Column1],[Table0Row1Column0,Table0Row1Column1]],[[Table1Row0Column0,Table1Row0Column1],[Table1Row1Column0,Table1Row1Column1]]]
/// - Empty string, when dataset contains no tables.
/// - Empty string, when all tables contain no rows.
/// </returns>
public string ToJson(DataSet dataSet)
{
if (dataSet == null) { throw new ArgumentNullException("dataSet"); }
StringBuilder result = new StringBuilder(string.Empty);
if (dataSet.Tables.Count > 0)
{
result.Append("[");
foreach (DataTable table in dataSet.Tables)
{
result.Append(ToJson(table));
}
result.Append("]");
}
return result.ToString();
}
/// <summary>
/// Converts a DataTable to a JSON string.
/// </summary>
/// <param name="table">The dataset to convert.</param>
/// <exception cref="System.ArgumentException">Thrown when parameter [table] is null.</exception>
/// <returns>
/// - JSON in the format JSON in the format [[Row0Column0,Row0Column1],[Row1Column0,Row1Column1]]
/// - Empty string, when datatable contains no rows.
/// </returns>
public string ToJson(DataTable table)
{
if (table == null) { throw new ArgumentNullException("table"); }
StringBuilder result = new StringBuilder(string.Empty);
if (table.Rows.Count > 0)
{
result.Append("[");
foreach (DataRow row in table.Rows)
{
result.Append(ToJson(row));
}
result.Append("]");
}
return result.ToString();
}
/// <summary>
/// Converts a DataRow to a JSON string.
/// </summary>
/// <param name="row">The data row to convert.</param>
/// <exception cref="System.ArgumentException">Thrown when parameter [row] is null.</exception>
/// <exception cref="System.ArgumentException">Thrown when property [DateTimeFormat] is null, empty or contains only whitespaces.</exception>
/// <returns>
/// - JSON in the format [Row0Column0,Row0Column1].
/// - Empty string, when datarow contains no columns.
/// </returns>
public string ToJson(DataRow row)
{
if (row == null) { throw new ArgumentNullException("row"); }
if (string.IsNullOrWhiteSpace(DateTimeFormat)) { throw new ArgumentNullException("DateTimeFormat"); }
StringBuilder result = new StringBuilder(string.Empty);
if (row.ItemArray.Count() > 0)
{
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(row.ItemArray);
// Replace Date(...) by a string in the format found in the property [DateTimeFormat].
var matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
var regex = new Regex(@"\\/Date\((-?\d+)\)\\/");
json = regex.Replace(json, matchEvaluator);
result.Append(json);
}
return result.ToString();
}
/// <summary>
/// Converts a JSON string to a object array.
/// </summary>
/// <param name="input">The input.</param>
/// <exception cref="System.ArgumentException">Thrown when input is null.</exception>
/// <returns></returns>
public object[] FromJson(string input)
{
if (input == null) { throw new ArgumentNullException("input"); }
var serializer = new JavaScriptSerializer();
object[] result = serializer.Deserialize(input, typeof(object[])) as object[];
return result;
}
/// <summary>
/// Replace JSON dates, like "\/Date(1330740183000)\/" to a string in the format found in the property [DateTimeFormat].
/// </summary>
/// <param name="match">When null, throws exception</param>
/// <exception cref="ArgumentNullException">Throws ArgumentNullException, when property [DateTimeFormat] is null, empty or contains only white spaces.</exception>
/// <returns>A string in the format found in the property [DateTimeFormat]</returns>
public string ConvertJsonDateToDateString(Match match)
{
if (match == null) { throw new ArgumentNullException("match"); }
if (string.IsNullOrWhiteSpace(DateTimeFormat)) { throw new ArgumentNullException("DateTimeFormat"); }
string result = string.Empty;
DateTime dt = new DateTime(1970, 1, 1); // Epoch date, used by the JavaScriptSerializer to represent starting point of datetime in JSON.
dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
dt = dt.ToLocalTime();
result = dt.ToString(DateTimeFormat);
return result;
}
}

 

Just an other tip on JSON, if you want to de-serialize a JSON string containing a object with field names, you can use:

string json = @"{Id:1,Barcode:""WWW12312345678""}";
var serializer = new JavaScriptSerializer();
var dict = serializer.Deserialize<Dictionary<string, string>>(json);
Console.WriteLine(dict["Barcode"]);

Dump all object properties to a string by using JSON in C#

If you want to dump the properties of an object into a string (lets say for logging purposes), you can use JSON serialization in C#. The output string is far less bulky than using XML serialization.

using System.Collections.Generic;
using System.Web.Script.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace NewCode.Rli
{
[TestClass]
public class NewCodeTester
{
[TestMethod]
public void Test()
{
var person = new Person
{
FirstName = "John",
Kids = new List<Person>
{
new Person { FirstName = "Kid", LastName = "One"},
new Person { FirstName = "Kid", LastName = "Two"}
},
LastName = "Do",
};
var serializer = new JavaScriptSerializer();
string output = serializer.Serialize(person);
/* Output: 
{
"FirstName":"John",
"LastName":"Do",
"Kids":
[
{
"FirstName":"Kid",
"LastName":"One",
"Kids":null
},
{
"FirstName":"Kid",
"LastName":"Two",
"Kids":null
}
]
}
*/
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Person> Kids { get; set; }
}
}