1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ASP.NET CoreでRazorコンポーネントのHTMLをコードで取得したい

Posted at

こんにちは。

テックリードのTerukiです。

たまにバックエンドで生成したHTMLをクライアントに送らずに取得したくなる場面が出てきます。

例えば、PDFにするためのHTMLをRazor等で書いた後にそれをPDFにしてくれるサービスに渡すなど。

今日はそれの紹介です。

結論

いきなりですがこれで取得できます。

// ServiceProviderとLoggerFactoryはDIから取ってくる
var htmlRenderer = new HtmlRenderer(ServiceProvider, LoggerFactory);

var html = await HtmlRenderer.Dispatcher.InvokeAsync(async () => {
    var dictionary = new Dictionary<string, object?> {
        ["Name"] = name,
        ["Description"] = description,
    };

    var parameters = ParameterView.FromDictionary(dictionary);
    var output = await htmlRenderer.RenderComponentAsync<ToBePdfComponent>(parameters).ConfigureAwait(false);

    return output.ToHtmlString();
}).ConfigureAwait(false);

ToBePdfComponent.razorファイル:

@code {
    [Parameter]
    public required string Name { get; set; }
    [Parameter]
    public required string Description { get; set; }
}

<h1>@Name</h1>
<p>@Description</p>

めっちゃシンプルです。Dispatcher辺りはややこしいですね。

この記事と組み合わせたらIP制限された環境からはどのURLにアクセスしても同じHTMLを返すなんてこともできそうです。

書くとしたらこんな感じでしょうか。

using var scope = serviceScopeFactory.CreateScope();
var htmlRenderer = new HtmlRenderer(scope.ServiceProvider, scope.ServiceProvider.GetRequiredService<ILoggerFactory>());

var html = await htmlRenderer.Dispatcher.InvokeAsync(async () =>
    (await htmlRenderer.RenderComponentAsync<IpRestriction403>()).ToHtmlString()); // Razorコンポーネントを用意

context.Response.Clear();
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(html);

だいぶ便利ですね。

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?