0

I'm trying to generate HTML to a string from a Razor component. I use the following code to do that:

var html = await _htmlRenderer.Dispatcher.InvokeAsync(async () =>
        {
            var pv = ParameterView.FromDictionary(parameters);
            var output = await _htmlRenderer.RenderComponentAsync<TComponent>(pv);

            return output.ToHtmlString();
        });

It worked well, until I included a child component in the Razor component and try to render it. I got the following error:

'HttpNavigationManager' has not been initialized

I don't use any navigation logic in the parent, nor in the child component. How can it be fixed?

3
  • Create and post a minimal reproducible example. Commented Apr 20 at 11:56
  • Component has a form tag with no action attribute set? Commented Apr 21 at 18:51
  • @GuidoNeele Yes, actually it has. What's "wrong" with that? How do that cause the error? Commented Apr 22 at 4:27

1 Answer 1

1

If your component has a Form tag without an action attribute the Razor renderer is trying to set the action to the current route. But since you are using static rendering the HtmlNavigationManager isn't initialized, which is used to set the route of the action attribute. You can prevent this by setting the action attribute yourself, maybe a # is enough (haven't tried that myself)

Sign up to request clarification or add additional context in comments.

2 Comments

Great explanation. Is it possible to manually initlialize the HtmlNavigationManager?
You can probably add ` services.AddScoped<NavigationManager, HttpNavigationManager>();` to your Program.cs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.