Aayush Rajopadhyaya
Aayush Rajopadhyaya

Reputation: 103

.NET Core Blazor Dynamically retreive html as string from Razor Page or Blazor Component

I have a Razor Page (.cshtml) which is a PDF Template. Now i am required to retreive html as string from the page itself to Save it as a PDF file. In MVC, i could use IRazonEngine interface for the same. But in Blazor what can used to obtain this.

Additionally, if i can use Blazor component instead of a Razor Page, how can i get the string of HTML?

Please help!

This is the sample Razor Page I have

@model UserModel

        <div>
            <strong>Full Name:</strong>
            <div>@Html.DisplayFor(model => model.FullName)</div>
        </div>
        
            <div>
                <strong>Address:</strong>
                <div>@Html.DisplayFor(model => model.Address);</div>
            </div>
            <div>
                <strong>DOB:</strong>
                <div>@Html.DisplayFor(model => model.DateOfBirth);</div>
            </div>

UPDATE : I have now used RazorLight.NETCore3 and working smooth.

Upvotes: 0

Views: 2566

Answers (1)

Umair
Umair

Reputation: 5481

I think this is not about blazor or mvc pages, but you can use RazorEngine.NetCore library to convert the cshtml and fill in the dynamic data using a model and retrieve the completed html.

Here is a guide on how to do it: https://khalidabuhakmeh.com/generate-outputs-with-razor-engine-in-dotnet-core

Github project: https://github.com/fouadmess/RazorEngine

Here is a simple code which I have used in the past:

public string CompileContent(string content, object model)
{
    var stringContent = Engine.Razor.RunCompile(content, Guid.NewGuid().ToString(), null, model);
    return stringContent;
}

Upvotes: 1

Related Questions