How to Change Base Type of Razor View Engine Pages

6 Mar
2011

In my previous blog entry (How to create custom HTML Helpers for ASP.NET MVC 3 and Razor View Engine) I mentioned extensibility point of Razor View Engine to change view/page base class type. It’s not a new approach for ASP.NET developers, we have been applying same pattern for ASP.NET Web Pages too.

You have to create a class which inherits from System.Web.Mvc.WebViewPage. You have to make it abstract class, so you don’t need to override abstract Execute method which inherits from WebPageExecutingBase, ASP.NET MVC runtime uses this method on runtime, actually creates an override method for this method at runtime, so if you override this method it will not change anything it will be overridden at runtime when ASP.NET create a type for your view/page.

    public abstract class DeveloqWebViewPage<T> : WebViewPage
    {
        public YourProjectSpecificHelper YourProjectSpecificHelper { get; set; }
        public YourCustomUiLibraryHelper YourCustomUiLibraryHelper { get; set; }

        public override void InitHelpers()
        {
            base.InitHelpers();
            YourProjectSpecificHelper = new YourProjectSpecificHelper();
            YourCustomUiLibraryHelper = new YourCustomUiLibraryHelper();
        }
    }


Helper types, these are basic C# classes there is no magic. I created different classes for different functionalities that I want to add because I prefer composition over inheritance if it is possible. (for more information about composition over inheritance read this article on c2 wiki)

    public class YourProjectSpecificHelper
    {
        public MvcHtmlString RenderIfInServingCountries(string htmlElement)
        {
            return new MvcHtmlString("do something magical");
        }

        public MvcHtmlString RenderCompanyTweets()
        {
            return new MvcHtmlString("do something magical");
        }
    }
    public class YourCustomUiLibraryHelper
    {
        public MvcHtmlString AddToolTipFor(string htmlElementId)
        {
            return new MvcHtmlString("do something magical");
        }
    }

We have created a base class for our Razor view pages. We can activate and use this base class with two ways. One is web.config based approach, you can change your base type from web.config file and don’t think about inheritance no more. It is a project-wide declaration.

You have to change pageBaseType attribute in pages element to active your new base page in your project.

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="RazorViewEngineExtensibility.DeveloqWebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Or you can change your base class for each page with separate declarations in your Razor page. Add @inherits declaration to the top of your Razor page.

@inherits RazorViewEngineExtensibility.DeveloqWebViewPage<object>

You need to give a generic type for your base class because it’s generic and need to be. If you don’t work with strongly typed view model you can just pass object as generic type.

Now we can access our newly added properties from Razor pages because it’s using new base class.

razor_base_type_change

 

Btw. you can always add new methods to YourCustomUiLibraryHelper and YourProjectSpecificHelper.These classes are not different than built-in HTML Helper in ASP.NET MVC.



1 Response to How to Change Base Type of Razor View Engine Pages

Avatar

Type-safe ViewBag use to pass generic view data to views / layouts | Robert Daniel Moore's Blog

July 10th, 2012 at 7:48 pm

[...] There is one thing I’ve missed out, which is to get Razor to compile using this new base page and to get intellisense working you have to either: [...]

Comment Form

top