Compilation error with attributes without value, in some cases
In any Razor component, add:
<video src="@videoSrc" autoplay></video>
@code{
string videoSrc = string.Empty;
}
In .NET 5 Preview 8,
error CS1503: Argument 2: cannot convert from 'string' to 'in Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrame'
<video src="movie.mp4" autoplay></video>
Compiles without error
dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.100-preview.8.20417.9
Commit: fc62663a35
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19041
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.100-preview.8.20417.9\
Host (useful for support):
Version: 5.0.0-preview.8.20407.11
Commit: bf456654f9
Yep, I'm getting exactly the same error with a Blazor Server Side app.
Most Visual Studio releases seem to break something these days, seriously, I'm not impressed! More wasted time.
Using Microsoft Visual Studio Community 2019 Preview Version 16.8.0 Preview 2.0
The following two lines are common amongst the errors I'm getting in the xxx.razor.g.cs files:
__builder.AddAttribute(15, "selected");
__builder.AddAttribute(12, "disabled");
In the code fragment below, its generating the error on the line:
<option value="@pageSize" selected>
<select id="pageSizeSelector" class="form-control-sm" FullWidth="false" @onchange="OnPageSizeChanged">
@foreach (var pageSize in AvailablePageSizes)
{
var isSelected = pageSize == PageSize;
@if (isSelected)
{
<option value="@pageSize" selected>
@pageSize
</option>
}
else
{
<option value="@pageSize">
@pageSize
</option>
}
}
</select>
Thanks for reporting this. I'm trying to track this down but so far haven't managed to reproduce the issue. Here's what I've tried:
dotnet --version
and verified the output is 5.0.100-preview.8.20417.9
dotnet new blazorserver
dotnet run
(and verified it works in a browser)Pages/Index.razor
to contain the following:@page "/"
<video src="@videoSrc" autoplay></video>
@code{
string videoSrc = string.Empty;
}
dotnet build
, which completed without errors. Also dotnet run
works.Pages/Index.razor
to contain:@page "/"
<select id="pageSizeSelector" class="form-control-sm" FullWidth="false" @onchange="OnPageSizeChanged">
@foreach (var pageSize in AvailablePageSizes)
{
var isSelected = pageSize == PageSize;
@if (isSelected)
{
<option value="@pageSize" selected>
@pageSize
</option>
}
else
{
<option value="@pageSize">
@pageSize
</option>
}
}
</select>
@code {
void OnPageSizeChanged() {}
int[] AvailablePageSizes = new[] { 1, 2, 3 };
int PageSize = 1;
}
dotnet build
, which completed without errors. Also dotnet run
works.So I'm not sure how to reproduce the problem. I also tried building in VS 2019 16.8.0 Preview 1.0 which worked fine too. I'll try Preview 2 later.
@sjohag @as-srandall Do either of you have further suggestions on how to reproduce the issue? If you follow the steps I listed above, does the error occur for you, or does it only happen in some specific project?
If it only happens in an existing project, can you try deleting your project's obj
/bin
directories and building again?
Using Visual Studio Version 16.8.0 Preview 2.0, File - New - Project - Blazor App - Blazor Server App
In .csproj target framework is netcoreapp3.1 and the problem can be reproduced.
Changing to net5.0 solves the problem.
I had
<td "table-primary"
that I changed to <td class="table-primary"
and one of the errors went away.
The other errors seemd non-legit.
Threw away obj and bin and built successfully.
Woops, spoke too early. It built but it didn't run! Same errors again. (What's that all about?)
Upon further analysis :)
The other five errors were actually legit, but the error message is misleading to say the least.
I had several td class=""
When I removed the empty class attribute, the errors went away.
@23min it's a little difficult to keep track of what you're saying. Could you clarify by posting a complete example of something that fails to compile with the error Argument 2: cannot convert from 'string' to 'in Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrame'
, if you saying you have such an example? Thanks!
Also, if you're getting a different error, then please post it as a different issue instead of here.
I had the same issue.
Each file with an error contained a html tag which had class="" in it. Once I removed all of those, my program is running again.
@SteveSandersonMS to sum it up:
In each case, I had a table definition <td>
with an invalid class or an attribute that was not recognized.
For example (invalid class)
and (empty class)
Does that clarify?
Just ran into this as updating to Version 16.8.0 preview 2
As @jeffrey-caldwell and @23min pointed out any blank attribute will trigger this. This also includes stuff like
<option value="">
For @23min's example if you need to keep the custom attribute you could just assign a string to it to keep it.
<td table-primary="table-primary">
Cheers to @jeffrey-caldwell and @23min for pointing this out. 馃憤
This also effects stuff like:
<textarea readonly>
Which as far as I know is an empty attribute.
@Zelif I just encountered this issue with VS 2019 16.8.0 preview 2.
For the
Thanks for reporting this. I'm trying to track this down but so far haven't managed to reproduce the issue
I've figured out what the cause is - if you've updated to .NET Core 5.0 Preview 8 SDK, but build an .NET Core 3.1 Blazor app that has value-less attributes, it creates the error. I had the same issue yesterday.
Steps to reproduce:
<TargetFramework>netcoreapp3.1</TargetFramework>
in project file<input type="text" @bind="value" required />
@code {
string value = "hello";
}
Severity Code Description Project File Line Suppression State
Error CS1503 Argument 2: cannot convert from 'string' to 'in Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrame' BlazorApp2 C:\Users\Howard\Source\Repos\BlazorApp1\BlazorApp2\obj\Debug\netcoreapp3.1\Razor\Pages\Index.razor.g.cs 84 Active
If you then edit the target framework to 5.0 it fixes the error.
You can also assign values as a workaround, e.g. required="required"
although that's not ideal
Another fix I've just tested, which avoids any changes to the HTML is to add a global.json
file to force use of the .NET 3.1 SDK for the build:
global.json
to the root of the solution folder (assuming you don't already have one){
"sdk": {
"version": "3.1.401",
"rollForward": "latestMinor"
}
}
If you don't know what SDK version you have installed, use
dotnet --list-sdks
at the command line
I have project:
<TargetFramework>netcoreapp3.1</TargetFramework>
I think I got this error after updating to Microsoft Visual Studio Community 2019 Preview Version 16.8.0 Preview 2.0
Helpfully workaround is working 馃挴 :)
Thanks everyone for the reports and the additional info.
We now have a fix pending for this at https://github.com/dotnet/aspnetcore/pull/25307. I expect this will ship in the 5.0 RC1 release.
Until then, the workaround I'd recommend is what @conficient suggested here, i.e., add a global.json
file to tell dotnet
to use the 3.x SDK (or a 5.0-preview7 or earlier one). This way you don't have to change your .razor
code at all.
The other good solution is to update fully to 5.0-preview8, i.e., change your TargetFramework
to 5.0, though that's a more dramatic fix as then you're really using a prerelease framework at runtime.
Most helpful comment
Another fix I've just tested, which avoids any changes to the HTML is to add a
global.json
file to force use of the .NET 3.1 SDK for the build:global.json
to the root of the solution folder (assuming you don't already have one)If you don't know what SDK version you have installed, use
at the command line