19 February, 2015

Error pages in DD4T (404 and 500)

In DD4T the 404 page can be retrieved as below, Override the Action Page, and when the Page is null return the NotFound Action. In the NotFound action, Query the Broker for the 404  page and return the view.


The 500 Error page can be throw from the Global level as below, when error occurred handle it the global level and return just view with error information



23 January, 2015

DD4TFormRouteHandler (posting a form as tridion page url)

ASP.NET routing enables us to use URLs that are not physical files, In DD4T we have the default page route definition. In which all page request redirct to Page controller and process the page.
DD4TFormRouteHandler is a custom route handler responsible for mapping incoming browser requests to particular MVC controller actions, this works along with DD4TFormHelper (that generate the route information for the form)
Posting a form in DD4T is not complicated, you can create the mvc form as a normal controller and action, then post it via AJAX. But, when we need to do post the form as normal page, It would need a tweak as the controller/action is not a page existed in tridion. This can be achieved by implementing a custom Mvc RoutHandler and reroute the posted form to the encrypted action and controller. It works as below daigram.

So, how to do this. to render out the form we have BeginDD4TForm html helper as below that generate the form with encrypted route values.
@using (Html.BeginDD4TForm("Post""Contact"FormMethod.Post))
{ 
    @Html.TextBox("username")
    <input type="submit" name="submit" value="Submit" />
}
BeginDD4TForm is a helper in the custom class DD4TFormHelper, which is responsible for generating encrypted route information.
Once this is done, then we need to add the custom handler to read the route information and route to the proper controller and action instead of the tridion page, this is the responsibilty of the class DD4TFormRouteHandler. To use this add this handler in your default page router as below.
  //Tridion page route
            routes.MapRoute(
               "TridionPage",
               "{*PageId}",
               new { controller = "Page"action = "Page" }, // Parameter defaults
               new { pageId = @"^(.*)?$" } // Parameter constraints
            ).RouteHandler = new DD4TFormRouteHandler();
This is the important step (Register the custom route handler)

In the action of form, once form is posted redirect to the current URL, so that it will load the Tridion page once the form is posted.

Both the helper and RouteHandler source code is below.

DD4TFormRouteHandler.cs
DD4TFormHelper.cs

17 January, 2015

Handling images and on the fly re-sizing in DD4T

When it comes with DD4T we have plenty of control on the data and we can decided on how we wanted the page to be rendered.
The traditional approach of having difference size of same image is replaced in DD4T to have one single image and generate the thumbnail on the fly. In this approach, we will have only one image published in tridion, and rest will be created on the fly based on the request.

Just by adding below configuration on the web.config modules session will enable this feature
<modules runAllManagedModulesForAllRequests="true">
  <add name="BinaryModule" 
       type="DD4T.Web.Binaries.BinaryDistributionModule" />
 </modules>
The BinaryDistributionModule process the request as below
  1. Process the Binary Request
  2. Check if the file is already in the file system.
    1. If file is available compare the last published date of the binary against the broker.
    2. If the file is updated, update the existing file in the file system.
    3. If the file is removed, delete the existing file in the file system.
  3. If the file is not there in the file system.
    1. Query the broker and create the file on local file system
  4. Deliver the file.

The below Html helper method helps to generate thumbnails as below.
<img src="@Model.Fields["image"].LinkedComponentValues[0].Multimedia.Url.ResizeToWidth(150)" />
<img src="@Model.Fields["image"].LinkedComponentValues[0].Multimedia.Url.ResizeToWidthAndHeight(150,150)" />
This will generate the images as
<img src=" /images/sample_image_tcm4-102_w150.jpg" />
<img src=" /images/sample_image_tcm4-102_w150_h150.jpg" />

The width and height feature has a bug in the framework, you can extract the BinaryDistributionModule and BinaryFileManager Class from the DD4T.Web project and bit customization on the method "private string StripDimensions(string path, out Dimensions dimensions)" will solve the issue, This method can be found in BinaryFileManager.cs. Update this method as in below would work for both width and height.

Later update the web.config to use the updated custom Binary Module, instead of default one,