asp.netasp.net-mvcasp.net-mvc-3blog-engineasp.net-mvc-4

Theme Selector idea for a Blog Engine written with ASP.NET MVC


I have started to build a blog engine which is totally unprofesional and meant to be not used by anyone. So, in plain English I cannot tell that you go ahead and run this for yourself and you will be happy.

You may see the complete code I have written so far :

https://github.com/tugberkugurlu/MvcBloggy

While now I am working on DAL, I also try to lay down what I need to do. One point I am stuck here is how I can handle theme selection for the blog engine.

I am not sure any of you guys has ever done something like this so far. I would appreciate if you can provide a way.


Solution

  • I suggest you to look at NBlog temable blog engine

    https://github.com/ChrisFulstow/NBlog

    In particular, look at the class ThemeableRazorViewEngine.cs

    https://github.com/ChrisFulstow/NBlog/blob/master/NBlog.Web/Application/Infrastructure/ThemeableRazorViewEngine.cs

    using System.Web.Mvc;
    using NBlog.Web.Application.Service;
    
    namespace NBlog.Web.Application.Infrastructure
    {
    public class ThemeableRazorViewEngine : RazorViewEngine
    {
        private readonly IThemeService _themeService;
    
        public ThemeableRazorViewEngine(IThemeService themeService)
        {
            _themeService = themeService;
    
            base.ViewLocationFormats = new[]
            {
                _themeService.Current.BasePath + "/Views/{1}/{0}.cshtml",
                _themeService.Current.BasePath + "/Views/Shared/{0}.cshtml",
                "~/Themes/Default/Views/{1}/{0}.cshtml"                
            };
    
            base.PartialViewLocationFormats = new string[] {
                _themeService.Current.BasePath + "/Views/{1}/{0}.cshtml",
                _themeService.Current.BasePath + "/Views/Shared/{0}.cshtml",
                "~/Themes/Default/Views/Shared/{0}.cshtml"
            };
        }
    
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {           
            // bypass the view cache, the view will change depending on the current theme
            const bool useViewCache = false;
    
            return base.FindView(controllerContext, viewName, masterName, useViewCache);
        }
    }
    }