Troubleshoot ASP.NET Core Blazor forms
Note
This isn't the latest version of this article. For the current release, see the .NET 8 version of this article.
Warning
This version of ASP.NET Core is no longer supported. For more information, see .NET and .NET Core Support Policy. For the current release, see the .NET 8 version of this article.
Important
This information relates to a pre-release product that may be substantially modified before it's commercially released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
For the current release, see the .NET 8 version of this article.
This article provides troubleshooting guidance for Blazor forms.
Large form payloads and the SignalR message size limit
This section only applies to Blazor Web Apps, Blazor Server apps, and hosted Blazor WebAssembly solutions that implement SignalR.
If form processing fails because the component's form payload has exceeded the maximum incoming SignalR message size permitted for hub methods, the form can adopt streaming JS interop without increasing the message size limit. For more information on the size limit and the error thrown, see ASP.NET Core Blazor SignalR guidance.
In the following example a text area (<textarea>
) is used with streaming JS interop to move up to 50,000 bytes of data to the server.
Add a JavaScript (JS) getText
function to the app:
window.getText = (elem) => {
const textValue = elem.value;
const utf8Encoder = new TextEncoder();
const encodedTextValue = utf8Encoder.encode(textValue);
return encodedTextValue;
};
For information on where to place JS in a Blazor app, see JavaScript location in ASP.NET Core Blazor apps.
Due to security considerations, zero-length streams aren't permitted for streaming JS Interop. Therefore, the following StreamFormData
component traps a JSException and returns an empty string if the text area is blank when the form is submitted.
StreamFormData.razor
:
@page "/stream-form-data"
@inject IJSRuntime JS
@inject ILogger<StreamFormData> Logger
<h1>Stream form data with JS interop</h1>
<EditForm FormName="StreamFormData" Model="this" OnSubmit="Submit">
<div>
<label>
<textarea> value streamed for assignment to
<code>TextAreaValue (<= 50,000 characters)</code>:
<textarea @ref="largeTextArea" />
</label>
</div>
<div>
<button type="submit">Submit</button>
</div>
</EditForm>
<div>
Length: @TextAreaValue?.Length
</div>
@code {
private ElementReference largeTextArea;
public string? TextAreaValue { get; set; }
protected override void OnInitialized() =>
TextAreaValue ??= string.Empty;
private async Task Submit()
{
TextAreaValue = await GetTextAsync();
Logger.LogInformation("TextAreaValue length: {Length}",
TextAreaValue.Length);
}
public async Task<string> GetTextAsync()
{
try
{
var streamRef =
await JS.InvokeAsync<IJSStreamReference>("getText", largeTextArea);
var stream = await streamRef.OpenReadStreamAsync(maxAllowedSize: 50_000);
var streamReader = new StreamReader(stream);
return await streamReader.ReadToEndAsync();
}
catch (JSException jsException)
{
if (jsException.InnerException is
ArgumentOutOfRangeException outOfRangeException &&
outOfRangeException.ActualValue is not null &&
outOfRangeException.ActualValue is long actualLength &&
actualLength == 0)
{
return string.Empty;
}
throw;
}
}
}
@page "/stream-form-data"
@inject IJSRuntime JS
@inject ILogger<StreamFormData> Logger
<h1>Stream form data with JS interop</h1>
<EditForm Model="this" OnSubmit="Submit">
<div>
<label>
<textarea> value streamed for assignment to
<code>TextAreaValue (<= 50,000 characters)</code>:
<textarea @ref="largeTextArea" />
</label>
</div>
<div>
<button type="submit">Submit</button>
</div>
</EditForm>
<div>
Length: @TextAreaValue?.Length
</div>
@code {
private ElementReference largeTextArea;
public string? TextAreaValue { get; set; }
protected override void OnInitialized() =>
TextAreaValue ??= string.Empty;
private async Task Submit()
{
TextAreaValue = await GetTextAsync();
Logger.LogInformation("TextAreaValue length: {Length}",
TextAreaValue.Length);
}
public async Task<string> GetTextAsync()
{
try
{
var streamRef =
await JS.InvokeAsync<IJSStreamReference>("getText", largeTextArea);
var stream = await streamRef.OpenReadStreamAsync(maxAllowedSize: 50_000);
var streamReader = new StreamReader(stream);
return await streamReader.ReadToEndAsync();
}
catch (JSException jsException)
{
if (jsException.InnerException is
ArgumentOutOfRangeException outOfRangeException &&
outOfRangeException.ActualValue is not null &&
outOfRangeException.ActualValue is long actualLength &&
actualLength == 0)
{
return string.Empty;
}
throw;
}
}
}
If form processing fails because the component's form payload has exceeded the maximum incoming SignalR message size permitted for hub methods, the message size limit can be increased. For more information on the size limit and the error thrown, see ASP.NET Core Blazor SignalR guidance.
EditForm parameter error
InvalidOperationException: EditForm requires a Model parameter, or an EditContext parameter, but not both.
Confirm that the EditForm assigns a Model or an EditContext. Don't use both for the same form.
When assigning to Model, confirm that the model type is instantiated.
Connection disconnected
Error: Connection disconnected with error 'Error: Server returned an error on close: Connection closed with an error.'.
System.IO.InvalidDataException: The maximum message size of 32768B was exceeded. The message size can be configured in AddHubOptions.
For more information and guidance, see the following resources:
ASP.NET Core