Search code examples
.netnancyviewengine

Custom Nancy viewEngine


I am trying to build a view engine on top of the "Super simple view engine" with additional functionality. Everything is fine except how to do what only my engine will be executed?

As i understand, Nancy searches all assemblies and loads all view engines from that. But how to prevent that and use only specified engine? Because now it executed two times, first time by default and second time in my engine:

My engine code:

public class LocalizableViewEngine : IViewEngine
{
    private readonly SuperSimpleViewEngineWrapper _ssengine;

    public LocalizableViewEngine(IEnumerable<ISuperSimpleViewEngineMatcher> matchers)
    {
        _ssengine = new SuperSimpleViewEngineWrapper(matchers);
    }

    public void Initialize(ViewEngineStartupContext viewEngineStartupContext)
    {
        _ssengine.Initialize(viewEngineStartupContext);
    }

    public Response RenderView(ViewLocationResult viewLocationResult, dynamic model, IRenderContext renderContext)
    {
        return _ssengine.RenderView(viewLocationResult, model, renderContext);
    }

    public IEnumerable<string> Extensions
    {
        get { return _ssengine.Extensions; }
    }
}

Solution

  • Found the answer myself. You just need to override ViewEngines property in your custom bootstrapper:

    public class Bootstrapper : DefaultNancyBootstrapper
    {
        protected override IEnumerable<Type> ViewEngines
        {
            get { yield return typeof (LocalizableViewEngine); }
        }
    }