Skip to content

Instantly share code, notes, and snippets.

@joeriks
Created April 4, 2012 10:00
Show Gist options
  • Save joeriks/2300068 to your computer and use it in GitHub Desktop.
Save joeriks/2300068 to your computer and use it in GitHub Desktop.
Add this file to your umbraco 4.7 App_Code and you will be able to add json-returning functions to base-urls
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using umbraco.presentation.umbracobase;
using System.Web.Script.Serialization;
namespace BaseAsJson
{
/// <summary>
///
/// Add this file to your umbraco 4.7 App_Code and you will be able to add json-returning functions to base-urls directly in your razor macros
///
///
/// Example 1. Set url-aware session variable value (for the current usersession)
///
/// Add this code in a razor-script:
///
/// Session["/base/asjson/session/value/"] = "can I haz";
///
/// Then you can use that url (/base/asjson/session/value/) to get that value ("can I haz")
///
/// you can use this for dynamic values or complex objects aswell, like an array of values, for example for some auto-complete
///
/// Session["/base/asjson/session/array/"] = new[] {"one", "two", "three"};
///
///
/// Example 2. Set url-aware session function (for the current usersession)
///
/// Add this code in a razor-script:
///
/// Session["/base/asjson/session/func1/"] = new Func<object>(()=>
/// {
/// return new { xxx="some string", currentdate=DateTime.Now.ToShortDateString()};
/// });
///
/// Then you can use that url (/base/asjson/session/func1/) to get that json { xxx:"some string", currentdate:"2012-04-02" }
///
///
/// Example 3. Set url-aware session function with parameter (for the current usersession)
///
/// Add this code in a razor-script:
///
/// Session["/base/asjson/session/getnode/"] = new Func<string, object>((param)=>
/// {
/// var node = new umbraco.NodeFactory.Node(Convert.ToInt32(param));
/// return new {name = node.Name};
/// });
///
/// Then you can use that url (/base/asjson/session/getnode/1065) to get that json { name:"Home Page" }
///
///
/// Example 4. Set url-aware application variable value (for any user)
///
/// There are also a possiblity to add the functionality to the App object to make it useable outside of session context. (Works with all examples 1,2,3, just change session to app)
///
/// Add this code as a razor-macro in the master template - or any page that is likely to get requested - for the App value to be initialized:
///
/// if ("App["/base/asjson/app/value/"]==null) { App["/base/asjson/app/value/"] = "can I haz"; }
///
/// Then you can use that url (/base/asjson/app/value/) to get that value
///
///
///
/// </summary>
[RestExtension("asjson")]
public class asjson
{
private static void writeObject(object obj, string parameter = "")
{
object result;
if (obj != null)
{
if (obj is Func<string, object>)
result = ((Func<string, object>)obj).Invoke(parameter);
else if (obj is Func<object>)
result = ((Func<object>)obj).Invoke();
else
result = obj;
}
else
result = null;
HttpContext.Current.Response.ContentType = "application/json";
var js = new JavaScriptSerializer();
HttpContext.Current.Response.Write(js.Serialize(result));
}
[RestExtensionMethod(returnXml = false)]
public static void app(string target, string parameter)
{
target = "/base/asjson/app/" + target + "/";
writeObject(HttpContext.Current.Application[target], parameter);
}
[RestExtensionMethod(returnXml = false)]
public static void session(string target, string parameter)
{
target = "/base/asjson/session/" + target + "/";
writeObject(HttpContext.Current.Session[target], parameter);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment