Disabling shaver toolbox

Is there a way to disable page tools in Razor? I have a problem when I try to set the razor template delegate to _AppStart and then use it in other pages. This will work well, besides the toolkit it keeps a reference in the BeginContext / EndContext to _AppStart, which explodes on other pages.

For example:

// TemplateHolder.cs
public static class TemplateHolder
{
    public static Func<object, object> TheTemplate = null;
}

@* _AppStart.cshtml *@
@{
    TemplateHolder.TheTemplate = @<b>the template</b>;
}

@* OtherPage.cshtml *@
@TemplateHolder.TheTemplate(Model);

      

Everything will work fine until you try to render the template in OtherPage.cshtml, at which point you get errors from within the toolkit trying to get the HttpContext for _AppStart.

Easy enough to fix this with a little reflection:

static readonly PropertyInfo _instrumentationService = typeof(WebPageExecutingBase).GetProperty("InstrumentationService", BindingFlags.NonPublic | BindingFlags.Instance);
static readonly PropertyInfo _isAvailableProperty = typeof(InstrumentationService).GetProperty("IsAvailable");

public static void DisableInstrumentation(this WebPageExecutingBase page)
{
    _isAvailableProperty.SetValue(_instrumentationService.GetValue(page), false);
}

      

... which prevents BeginContext / EndContext calls from being processed.

But wasn't all of Razor's meaning object-oriented and customizable? I feel like I am missing something.

+3


source to share





All Articles