soundaranbu / Razor.Templating.Core

Razor Templating Engine to render Razor Views(.cshtml files) to String in Console, Web, Service, Desktop workloads in .NET Core 3+

Home Page:https://soundaranbu.medium.com/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WebRootPath in debug mode

stefanodelpero opened this issue · comments

Hi! Great library!

I've a problem with the WebRootPath in debug in Visual Studio.
WebRootPath point to the bin folder that do not contains wwwroot, instead of the wwwroot inside the source folder (as IWebHostEnvironment.WebRootPath does).

I'm missing something?

Thank you!

Hi @stefanodelpero, the library internally computes the path for the internal usage. But that should not interfere with mvc behaviour I believe.
Can you give more details please? Some code snippets will also help.

Hi @soundaranbu,
I use a TagHelper in the cshtml that I use to generate HTML with Razor.Templating.Core.

<img write-content="true" src="~/images/logo-header.png" class="mb-4" />

Normally in the application (when in Debug from Visual Studio) the IWebHostEnvironment.WebRootPath points to the wwwroot in the source folder of Visual Studio.

Instead, in the WriteContentTagHelper, I have an IWebHostEnvironment object (from DI) that is created from Razor.Templating.Core.
Here, the WebRootPath property point to the \bin\Debug\net7.0 folder.

public WriteContentTagHelperBase(IWebHostEnvironment environment)
{
    var path = environment.WebRootPath;
    // Path ends with \bin\Debug\net7.0
}

I think this is because DirectoryHelper.GetWebRootDirectory search for the wwwroot in the assembliesBaseDirectory.
But in Debug mode in Visual Studio the assembliesBaseDirectory do not have the wwwroot folder.

Hi @stefanodelpero, thanks for this. I've pushed a fix. I hope this will fix your issue. I'll let you know when a pre-release package is published.

@soundaranbu It do not works. Because the parameter services of the ServiceCollectionExtensions.AddRazorTemplating is a new object created in RazorTemplateEngine.CreateInstance.

Thanks @stefanodelpero.

If i understand the problem correctly, then you should be receiving the default IWebHostEnvironment object provided by ASP.NET Core. The default object should have the correct path set retaining the original behaviour of the framework. This is considering your application is MVC.

Please provide a minimal sample if possible. We can take it forward from there.

Thanks!

I made only two changes to make it work in your sample:

In Program.cs, add to DI

builder.Services.AddRazorTemplating();

In .csproj, use the pre-release version

    <PackageReference Include="Razor.Templating.Core" Version="1.9.0-rc.2" />

Result:

image

Actually, the pre-release didn't make any difference. The latest stable v1.8.0 itself is working fine after adding to DI.

Oh man. Sorry to waste your time!
I've been using your library for a long time and never needed to add DI. So I didn't think about it.
Thanks a lot!