我有一个。net 6 Blazor服务器应用程序,我已经工作了一段时间。昨晚当我关闭Visual Studio时,它更新到最新版本(此时为17.2.4)。当我第二天回到这个项目并试图编译这个项目时,我得到了一堆奇怪的错误(以前我没有):
错误CS0116命名空间不能直接包含诸如字段、方法或statements D:RepositoriesMyWebAppMyWebApp. web Microsoft.NET.Sdk.Razor.SourceGeneratorsMicrosoft.NET.Sdk.Razor.SourceGenerators. razorsourcegenerator Pages_Sample_razor.g.cs 356
错误CS0246类型或命名空间名称'await'找不到(您是否缺少using指令或程序集参考?)SuggestionApp。网络D: 库页面 MyWebApp MyWebApp.Web 样本。剃须刀67
错误CS8802只有一个编译单元可以具有顶级语句。SuggestionApp。网络D: 库页面 MyWebApp MyWebApp.Web 样本。剃须刀89
应用程序前一天工作得很好,但现在我得到了20个这样的错误。我没有触及自动生成文件中的任何内容,也没有对所讨论的blazor组件进行任何更改Sample.razor.
我试着清理解决方案,我试着创建一个不同的文件,但我的尝试都没有成功。
我猜这个问题是由VS更新引起的,即使在发布说明中没有关于blazor的任何内容。我是唯一一个有这个问题的人,还是有一个解决这类问题,我不知道?
我之所以得到上面提到的所有这些奇怪的错误,是因为blazor错误地解释了缩进。这是抛出所有异常的文件Sample.razor
@page "/sample"
@inject ICategoryData categoryData
@inject IStatusData statusData
@inject IUserData userData
@inject ISuggestionData suggestionData
<h3>Sample Data</h3>
@if (categoriesCreated)
{
<h4>Categories have been created</h4>
}
else
{
<button class="btn btn-primary" @onclick="CreateCategories">Create
Categories</button>
}
@if(statusesCreated)
{
<h4>Statuses have been created</h4>
}
else
{
<button class="btn btn-primary" @onclick="CreateStatuses">Create
Statuses</button>
}
<button class="btn btn-danger" @onclick="GenerateSampleData">Generate
Sample Data</button>
@code {
private bool categoriesCreated = false;
private bool statusesCreated = false;
private async Task GenerateSampleData()
{
UserModel user = new()
{
FirstName = "Florin",
LastName = "Zamfir",
EmailAddress = "florin@test.com",
DisplayName = "TestUser",
ObjectIdentifier = "test"
};
await userData.CreateUser(user);
var foundUser = await userData.GetUserFromAuthentication("test");
var categories = await categoryData.GetAllCategories();
var statuses = await statusData.GetAllStatuses();
HashSet<string> votes = new();
votes.Add("1");
votes.Add("2");
votes.Add("3");
votes.Add("4");
SuggestionModel suggestion = new()
{
Author = new BasicUserModel(foundUser),
Category = categories[0],
Suggestion = "Our First Suggetion",
SuggestionStatus = statuses[1],
UserVotes = votes,
Description = "This is a suggestion created by the sample data
generation method",
ApprovedForRelease = true
};
// For some reason blazor interpreted this as the end of the code block
// hence the weird errors like "Type or namespace 'await' cannot be found"
await suggestionData.CreateSuggestion(suggestion);
suggestion = new()
{
Author = new BasicUserModel(foundUser),
Category = categories[0],
Suggestion = "Our Second Suggetion",
SuggestionStatus = statuses[2],
Description = "This is a suggestion created by the sample data
generation method",
ApprovedForRelease = true
};
await suggestionData.CreateSuggestion(suggestion);
suggestion = new()
{
Author = new BasicUserModel(foundUser),
Category = categories[1],
Suggestion = "Our Third Suggetion",
SuggestionStatus = statuses[3],
Description = "This is a suggestion created by the sample data
generation method",
ApprovedForRelease = true
};
await suggestionData.CreateSuggestion(suggestion);
suggestion = new()
{
Author = new BasicUserModel(foundUser),
Category = categories[2],
Suggestion = "Our Fourth Suggetion",
SuggestionStatus = statuses[1],
Description = "This is a suggestion created by the sample data
generation method",
ApprovedForRelease = true
};
await suggestionData.CreateSuggestion(suggestion);
}
private async Task CreateCategories()
{
var categories = await categoryData.GetAllCategories();
if (categories?.Count > 0) return;
CategoryModel cat = new()
{
CategoryName = "Bug",
CategoryDescription = "Report a bug"
};
await categoryData.CreateCategory(cat);
cat = new()
{
CategoryName = "Feature",
CategoryDescription = "Suggest a feature"
};
await categoryData.CreateCategory(cat);
categoriesCreated = true;
}
private async Task CreateStatuses()
{
var statuses = await statusData.GetAllStatuses();
if (statuses?.Count > 0) return;
StatusModel stat = new()
{
StatusName = "Completed",
StatusDescription = ""
};
await statusData.CreateStatus(stat);
stat = new()
{
StatusName = "Watching",
StatusDescription = ""
};
await statusData.CreateStatus(stat);
stat = new()
{
StatusName = "Upcoming",
StatusDescription = ""
};
await statusData.CreateStatus(stat);
stat = new()
{
StatusName = "Dismissed",
StatusDescription = ""
};
await statusData.CreateStatus(stat);
statusesCreated = true;
}
在GenerateSampleData()
方法中,我将注释放在代码blazor中,将其解释为代码块的结束。在设计时,它没有显示任何错误。一旦我尝试编译它,它就会发现有些地方不正确,并抛出所有异常。
我确实尝试了所有方法来修复它,最终重写了方法。我很惊讶,即使在删除obj
和bin
文件夹后,替换文件也不起作用。我想我得把这个bug报告给微软了。
您检查构建输出了吗?大多数情况下,它会提示哪个文件[指示失败]盯着看直到某物变得明显让我回到了我的PHP时代…
只是我的两分钱,但当我遇到这个,这是因为我不小心添加或遗漏了一个字符[{,},<,比;,;…在标记或其他地方,,特别是当它是razor.g.cs文件时。抱怨。
正如其他人所建议的,清理解决方案或删除bin和obj