Efficient JSON communication in ASP.NET MVC

A really short example on how ASP.NET MVC can be used for two-way JSON string communication.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using SenseNet.ContentRepository.Storage;
using SenseNet.ContentRepository.Storage.Security;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;

namespace SenseNet.Services.ContentStore
{
public class JsonFilter : ActionFilterAttribute
{
public string Param { get; set; }
public Type DataType { get; set; }

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(DataType);
var data = ser.ReadObject(filterContext.HttpContext.Request.InputStream);
filterContext.ActionParameters[Param] = data;

}
}
}
public class SecurityController : Controller
{

public ActionResult GetACL(string path)
{
var node = Node.LoadNode(path);
var acl = node.Security.GetAcl();
return Json(acl);
}

[AcceptVerbs(HttpVerbs.Post)]
[JsonFilter(DataType = typeof(SnAccessControlList), Param = "acl")]
public ActionResult SetACL(SnAccessControlList acl)
{
var node = Node.LoadNode(acl.Path);
node.Security.SetAcl(acl);
return null;
}
}

}

Pingbacks and trackbacks (1)+

Comments are closed

Welcome to the blog!

Sense/Net ECM is ever evolving. Community means the world to us so we want to keep you apprised on what’s happening on our side of the woods. Want to make us happy? Add a comment and tell us what you think!

Month List