0

I just switched from Umbraco 4 to Umbraco 5 and it seems like a lot has changed. So what i basically want is a possibility to add a alternative template to my document-types. The fall back template shall return the content as JSON.

Why, you say? I want to create an API-like way of accessing the Umbraco data from my mobile devices.

WebAPI (http://cultiv.nl/blog/2012/4/22/exposing-umbraco-5-content-through-the-aspnet-web-api/) I have thought about using WebAPI from asp.net MVC 4, but the project is really just a proof of concept and i don't want to code each endpoint.

So i found a som guys that did a package for Umbraco 4 that actually does this and renders the content of @currentPage as Json. The template is hit by adding "/JSON" to the end of the url. Unfortunetly this uses xslt, which ihas been removed from Ubraco 5.1 (Good thing).

So. I bet it's simple to create a partial, a macro or a partial macro that does this and add it to a template. But is just cant figure out where to start.

Any help with that? What I'm looking for is a step guide on what steps to take, to make the setup. Rendering the stuff ad json in C# i can handle. It's the integration into Umbraci I lack.

Hoe u can help.

4

1 回答 1

0

替代模板的工作方式与 v4 中的完全相同:只需在 url 末尾添加模板名称:yoururl/json

然后使用以下代码向名为“json”的模板添加一个新的 razor 视图:

@inherits RenderViewPage
@using System.Web.Mvc.Html;
@using Umbraco.Cms.Web;
@{
    Layout = "";
}
{
@foreach (var attribute in Model.Attributes)
{
    string.Format("\"{0}\": \"{1}\"", attribute.AttributeDefinition.Alias, attribute.DynamicValue);
}
}

此代码可以用作起点,而无需使用 web api 或自己的控制器。

不要忘记允许所有文档类型的模板

hth,托马斯

于 2012-05-21T08:25:18.293 回答