andrewlocatelliwoodcock

Thoughts on Software

Custom View Engines: Registering a custom extension and avoiding the “There is no build provider registered for the extension” error

with one comment


Once you’ve implemented a custom View Engine in ASP.Net MVC , and there will be subsequent posts covering how to do just this, there are another couple of steps to perform before your wonderful new view engine is wired up and rendering views.

One of the first things you may encounter is the dreaded “There is no build provider registered for the extension ‘.xxxx'” error. This is actually quite straightforward to fix and is one case where everything you need to know is actually included in the error message! Basically, ASP.Net MVC expects a <buildProviders> section that matches your new extension in the local web.config, machine-level web.config or machine.config and hasn’t found one. The example below shows how to fix this: add the relevant sections! In this case, we are registering two extensions and stating that the extensions should be interpreted with the Razor View Engine. These examples are actually the LoggingViewEngine and PdfViewEngine mentioned in my last post, both of which extend the RazorViewEngine to leverage its syntax and parsing and extend it by using the output in novel ways:

<system.web>

<compilation debug=”true” targetFramework=”4.0″>

<assemblies>

.

.

.

.

</assemblies>

<buildProviders>

<add extension=”.cslog” type=”System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor”/>

<add extension=”.cspdf” type=”System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor”/>

</buildProviders>

</compilation>

</system.web>

 

The second step is to tell ASP.Net MVC how to interpret these extensions and register the View Engines that will deal with them. And again, it’s simple once you know that it needs to be done 🙂

In the Global.asax.cs, add the highlighted lines below to the Application_Start method:

protected void Application_Start()

{

AreaRegistration.RegisterAllAreas();

 

            // add the new extensions to the collection of languages supported by Razor

            RazorCodeLanguage.Languages.Add(“cslog”, new CSharpRazorCodeLanguage());

            RazorCodeLanguage.Languages.Add(“cspdf”, new CSharpRazorCodeLanguage());

 

            // register the extensions

            WebPageHttpHandler.RegisterExtension(“cslog”);

            WebPageHttpHandler.RegisterExtension(“cspdf”);

 

RegisterGlobalFilters(GlobalFilters.Filters);

RegisterRoutes(RouteTable.Routes);

RegisterViewEngines(ViewEngines.Engines);

}

 

and add the following method:

public static void RegisterViewEngines(ViewEngineCollection viewEngines)

{

viewEngines.Clear();

viewEngines.Add(new RazorViewEngine());

viewEngines.Add(new LoggingViewEngine.LoggingViewEngine());

viewEngines.Add(new PdfViewEngine.PdfViewEngine());

}

 

And there you have it: we have registered two new extensions, told ASP.Net MVC to react to them and added the View Engines that know how to support them to the View Engines collection.

Simples!

 

Written by andrewlocatelliwoodcock

October 18, 2011 at 22:43

One Response

Subscribe to comments with RSS.

  1. […] a comment » In my last post, I discussed how to tell ASP.Net MVC about the custom extensions for your fancy new custom views […]


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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: