Skip to content

I think I might need to do all of the Razor imports for the custom template engine ... #2

Description

@phamquangquy92vd

I think I might need to do all of the Razor imports for the custom template engine ...

private static RazorSourceDocument GetDefaultImports()
{
    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream, Encoding.UTF8))
    {
        writer.WriteLine("@using System");
        writer.WriteLine("@using System.Collections.Generic");
        writer.WriteLine("@using System.Linq");
        writer.WriteLine("@using System.Threading.Tasks");
        writer.WriteLine("@using Microsoft.AspNetCore.Mvc");
        writer.WriteLine("@using Microsoft.AspNetCore.Mvc.Rendering");
        writer.WriteLine("@using Microsoft.AspNetCore.Mvc.ViewFeatures");
        writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TModel> Html");
        writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json");
        writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component");
        writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.IUrlHelper Url");
        writer.WriteLine("@inject global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider");
        writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor");
        writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.HeadTagHelper, Microsoft.AspNetCore.Mvc.Razor");
        writer.WriteLine("@addTagHelper Microsoft.AspNetCore.Mvc.Razor.TagHelpers.BodyTagHelper, Microsoft.AspNetCore.Mvc.Razor");
        writer.Flush();

        stream.Position = 0;
        return RazorSourceDocument.ReadFrom(stream, fileName: null, encoding: Encoding.UTF8);
    }
}

... and then specify those defaults ...

public CustomRazorTemplateEngine2(RazorEngine engine, RazorProject project) 
    : base(engine, project)
{
    Options.ImportsFileName = "_ViewImports.cshtml";
    Options.DefaultImports = GetDefaultImports();
}

However, I just moved a test app to 2.1, and it's breaking now. It looks like the new <body> Tag Helper must have a closing tag, but WebMarkupMin.Core removes that as part of its minification. Therefore, I'll probably need to escape the closing <body> tag.

I'm slammed at the moment, but I'll try to get back to this by Sunday and ping you back here. In the meantime, try to implement those defaults ☝️ and see if that works.

Originally posted by @guardrex in #1 (comment)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions