Creating a viewless, modelless test application for Tridion

When setting up a new Tridion environment for a client or for testing purposes, there is often a requirement for a quick sample web application. To verify all connections and configuration are set up correctly, or sometimes just to hit some URLs and see some information on the page. This can be useful for instance when dealing with a copied database or in the process of a Tridion upgrade to the latest version. This blog post will demonstrate the quickest and easiest way to get the basics up and running.

There are many tools to make setting up a web application faster and easier. Recently, Trivident released Dyndle, a framework layered on top of DD4T that takes away many of the complications of setting up a web-application with Tridion. There is even a helpful tool to generate views and models based on the schemas in your Tridion instance, making the process as automated as possible.

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!