Sense/Net 6.0 Devblog
The development blog of Sense/Net 6.0
Back to Sense/Net

Efficient JSON communication in ASP.NET MVC

January 20, 2010 19:59 by Peter Zentai

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;
}
}

}

Tags:

Categories: .Net
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Bookmark and Share