tobbbe
12/29/2015 - 1:23 PM

jsonp in razortemplate

if (Request.QueryString["jsonp"] != null)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.ContentType = "application/javascript";

        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

        var newsItems = Model.Content.Children.ForEach(x => new
        {
            Date = x.GetPropertyValue<DateTime>("ContentDate").ToString("yyyy-MM-dd"),
            Preamble = x.GetPropertyValue<string>("ContentPreamble"),
            Text = x.GetPropertyValue<string>("ContentText"),
            Image = x.HasValue("ListImage") ? Request.Url.GetLeftPart(UriPartial.Authority) + Umbraco.TypedMedia(x.GetPropertyValue<string>("ListImage")).Url : ""
        }).Reverse();

        var jsonResponse = serializer.Serialize(new
        {
            news = newsItems
        });

        Response.Write("news(" + jsonResponse + ")");

        Response.End();
    }