But what if even that is too much of a hassle? What if you want the most basic form of web-application to display some data from Tridion, without having to create any models or views for the content. Well, that is something I recently was looking for. The simple requirement of being able to hit any URL and see the id and title of the page if it exists in the broker.
So, how do I accomplish something like this? I have the abstraction of DD4T and Dyndle at my disposal to take care of all of the hard work behind the scenes. But how do I make it so I don’t have to create any models or views? Turns out, thanks to Dyndle, it is really quite simple.
So, I opened up Visual Studio and created a new .NET Framework web application. Following the Dyndle quick-start instructions, I quickly created a working web application. Firing it up, however, confronted me with an issue:
The view 'DefaultPage' or its master was not found or no view engine supports the searched locations. The following locations were searched:
Of course, without any views or models, my web-app wasn’t going to do much. That was the very problem I was trying to solve! So this was the interesting part. How could I leverage the content provided by Dyndle and DD4T and display it in the web-app? Well, that is where the magic of model binding came into play. I needed to do three things:
- Create a controller with a custom action
- Route every incoming request to it, and
- Make sure that the model binder of Dyndle would provide me with the page data
So I created a new controller with one action in it, then I opened up the route config and updated the default route to point to my controller action.
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{*page}", defaults: new { controller = "Home", action = "Page", page = UrlParameter.Optional } ); }
I changed the name of the default “id” parameter to “page” and then set up the url with a wild card for all urls: “*” followed by “page”. This will ensure every url will be matched and send to my controller, passing along the value of the url in a parameter named “page”.
Now I needed to add a parameter under the same name to the controller action with the “IWebPage” type (the interface that dyndle’s model-binder returns) so the model binding can do it’s magic. I changed the return type of the action to “ContentResult” so I could do:
public virtual ContentResult Page(IWebPage page)
{
return Content($"{page.Id}");
}
Requesting a page that is published would now output the page id in the browser!
Making it a bit nicer with a null check and some JSON serialization of the page’s model data.
if (page != null) { return Content(JsonConvert.SerializeObject(page.ModelData, Formatting.Indented), "application/json"); } else { return Content("Page not found"); }
We get the following result. Built-in JSON viewer from Firefox provides us with a nice graphical interface where we can inspect the entire JSON content of the page in a user-friendly manner. Similar functionality can also be found in Google Chrome through third-party plugins.
Now, we’ve got all we need to continue development on our Tridion environment and quickly demo content at the end of the chain in our test application. If you follow the steps, you will have your own test application in less than an hour. Fortunately, this demo app is available for download as well!