Using dynamic view markers in Sitecore MVC

While working with Sitecore, we add many renderings in the presentation and that makes up each page that is output for the corresponding item.

We are currently working with Sitecore MVC, and we have traditionally added start and end comments in views manually (copy pasting the path of the current view) to mark the start and end of each component on the page. This helped us debug things, made the output html more readable and maintainable, and lets face it, after a month or two, we find it hard to remember which module was called what!

Recently it was pointed out that this might be a potential security threat and we are exposing details of our folder structure in the production environment, which might not be the best idea.

So to ease the process, and remove this potential threat, we created a html helper extension method which would dynamically output the view path start and end comments subject to an appsetting value being toggled to true. This appsetting key would be toggled to false in any environment where we wouldn’t want the comments to be output!

        public static ViewMarker BeginViewMarker(this HtmlHelper htmlHelper, string filename)
        {
            htmlHelper.ViewContext.Writer.Write(RenderFilenameComment("START", filename));
            return new ViewMarker(htmlHelper.ViewContext, filename);
        }

        private static HtmlString RenderFilenameComment(string position, string filename)
        {
            if (ConfigurationManager.AppSettings["ShowViewMarkers"] == "true")
            {
                return new HtmlString(string.Format("<!-- {0}: {1} -->", position.ToUpper(CultureInfo.CurrentCulture), filename));
            }

            return new HtmlString("");
        }

        public class ViewMarker : IDisposable
        {
            private readonly TextWriter _writer;
            private readonly string _filename;
            public ViewMarker(ViewContext viewContext, string filename)
            {
                _writer = viewContext.Writer;
                _filename = filename;
            }

            public void Dispose()
            {
                _writer.Write(RenderFilenameComment("END", _filename));
            }
        }

And in the view, we would just enclose all the view html / razor within a call to this helper method:

@using (Html.BeginViewMarker(VirtualPath))
{
   ...
   ...
   ...
}

The output would look like:

2015-09-19_223640

Advertisements

One thought on “Using dynamic view markers in Sitecore MVC

  1. […] In an earlier post I had shown how we had partially resolved this by making the build up of the comment dynamic using a html helper method which used the WebPageExecutingBase.VirtualPath property from the System.Web.WebPages assembly. You can refer the post here: Using dynamic view markers in Sitecore MVC […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s