<< Previous Post
Next Post >>

Posted by admin under General  sBlog.Net v3.0  on Dec 01 2013

In this post, I would like to discuss the most recent check-in for sBlog.Net. With the latest update you will be able to create theme specific partial views and thus you are not limited by the partial views provided by the blog engine. For example, you could create a partial view that override's the way a post pager for the blog is displayed [used for navigating between posts]. For this to happen, a Shared folder has to be created within the theme's folder and then a partial view that correspond to the name (in this case PostPager.cshtml) of the partial view being overridden. This is possible because of a few changes to the SetupViewEngines method in the Application_Start event. It's given below:

   private void SetupViewEngines()
       var settings = InstanceFactory.CreateSettingsInstance();
       ViewEngines.Engines.Add(new CustomRazorViewEngine(settings.BlogTheme));

Note how an instance of CustomRazorViewEngine is passed to the ViewEngines.Engines.Add. CustomRazorViewEngine is a class that overrides RazorViewEngine. The main function of this class is to set the locations to be searched for finding a view, partial view or a master. Here is the complete listing:

    public class CustomRazorViewEngine : RazorViewEngine
        public CustomRazorViewEngine(string blogTheme)
            base.AreaViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            base.AreaMasterLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            base.AreaPartialViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            base.ViewLocationFormats = new[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
            base.MasterLocationFormats = new[] { "~/Views/{1}/{0}.cshtml", "~/Views/{1}/{0}.vbhtml", "~/Views/Shared/{0}.cshtml", "~/Views/Shared/{0}.vbhtml" };
            base.PartialViewLocationFormats = GetPartialViewLocations(blogTheme);
            base.FileExtensions = new[] { "cshtml", "vbhtml" };

        private static string[] GetPartialViewLocations(string blogTheme)
            var locations = new List<string>();

            if (HasSharedPartialViews(blogTheme))
                var csPath = "~/Themes/" + blogTheme + "/Shared/{0}.cshtml";
                var vbPath = "~/Themes/" + blogTheme + "/Shared/{0}.vbhtml";


            return locations.ToArray();

        private static bool HasSharedPartialViews(string blogTheme)
            if (string.IsNullOrEmpty(blogTheme))
                return false;

            var pathMapper = DependencyResolver.Current.GetService<IPathMapper>();
            var directoryRelativePath = string.Format("~/Themes/{0}/Shared", blogTheme);
            var directoryPath = pathMapper.MapPath(directoryRelativePath);
            return Directory.Exists(directoryPath);

The constructor takes care of setting the locations. The thing to note in the constructor is the call to GetPartialViewLocations. This method is used to "find" out if the current theme contains a Shared folder. If so, it's added as the first entry to the list. It has to be the first entry since that's the order used for searching by the MVC framework. Thus, using this approach, a partial view residing within the appropriate theme folder gets the priority over the one in the common shared views folder. Hope you enjoyed this short post! Happy coding!

share the love!


there are no comments for this post. be the first to post one!

post your comment

name (*) - required
email address (*) - required
site address
your comment (*) - required

(*) - required

<< Previous Post
Next Post >>