Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
phamquangquy92vd opened this issue Apr 25, 2021 · 0 comments

Comments

@phamquangquy92vd
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant