asp.net-corerazorblazorblazor-server-siderazorengine

'HttpNavigationManager' not initialized when rendering static HTML


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?


Solution

  • 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)