升级到 asp.net 核心 3.1 后逐字声明变量时出错
Error declaring verbatim variables after upgrade to asp.net core 3.1
将我的 Asp.Net Core MVC Webapp 解决方案从 .Net Core 2.2 迁移到 .Net Core 3.1 后,我遇到了一个特殊错误。
我按照官方docs中描述的步骤,更新Program,Startup,nuget packages ...
我正在使用 Syncfusion Essential Studio 2 - 我认为它与此错误无关 - 在许多视图中我声明 variables/creating 对象,例如创建一个 DropDownList 用于在数据网格:
@model CultureMultiModel
@{
var userCultLookupDDL = new Syncfusion.EJ2.DropDowns.DropDownList()
{
FilterType = Syncfusion.EJ2.DropDowns.FilterType.Contains,
Fields = new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings { Text = "Display", Value = "Name", GroupBy = "Parent" }
...
};
var userCultParams = new { @params = userCultLookupDDL };
这在 2.2 中运行良好,但现在我在 new { @params ...
上遇到错误
将鼠标悬停在@params 上时,会显示以下内容:
CS1525:无效的表达式项 'params'
CS1003: 语法错误,',' 预期
我已经尝试了很多事情:
- 使用圆括号@(params) 给出同样的错误
- 添加 nuget 包 Microsoft.AspNetCore.Mvc.Razor.Extensions,同样的错误
- 添加
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
到项目文件,同样的错误
- 更改为@Params 或@arams 会出现错误 CS0103:该名称在当前上下文中不存在
由于在编辑 cshtml 文件时立即 compilation/validation 发生这种情况,我认为这与 Startup 无关,但为了完整起见我还是添加了这个:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<myContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("myConnection"), builder => builder.MigrationsAssembly("myDAL")));
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddLocalization(options => options.ResourcesPath = "Resources");
LocalizationSupport localizationSupport = new LocalizationSupport();
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
options.SupportedCultures = localizationSupport.SupportedCultures;
options.SupportedUICultures = localizationSupport.SupportedCultures;
});
services.AddMemoryCache();
services
.AddMvc()
.AddViewLocalization()
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(SharedModelLocale));
})
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
;
services
.AddControllersWithViews()
.AddNewtonsoftJson(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddRazorPages();
services.Configure<IdentityOptions>(options =>
...
services.AddHttpContextAccessor();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
// << Localization
LocalizationSupport localizationSupport = new LocalizationSupport();
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en"),
SupportedCultures = localizationSupport.SupportedCultures,
SupportedUICultures = localizationSupport.SupportedCultures,
};
app.UseRequestLocalization(options);
// >> Localization
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapAreaControllerRoute(name: "IdentityRoute", areaName: "Identity", pattern: "{area:exists}/{controller=Home}/{action=Index}");
endpoints.MapDefaultControllerRoute();
});
}
和我的项目文件
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Markdig" Version="0.22.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.15" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" PrivateAssets="All" />
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="19.1.0.59" />
<PackageReference Include="System.ServiceModel.Http" Version="4.5.3" />
</ItemGroup>
这可能是一件微不足道的事情,但在任何文档中都没有提到这一点,而且我的搜索也没有返回任何结果。
我已经准备在 JS 中重写 C# 代码,但 Syncfusion 支持提供了正确答案:双重转义。
所以正确的语法是:
var userCultParams = new { @@params = userCultLookupDDL };
虽然这对许多人来说似乎合乎逻辑,但我没有意识到这将是解决方案,甚至没有尝试过。
我确实在其他帖子中读到过它,但无法将其与我的情况相匹配。我的理由是:我已经将关键字 params
转义为 @params
也许其他人 运行 像我一样喜欢这个(并且他们的头靠在墙上)所以我认为分享这个是明智的。
将我的 Asp.Net Core MVC Webapp 解决方案从 .Net Core 2.2 迁移到 .Net Core 3.1 后,我遇到了一个特殊错误。
我按照官方docs中描述的步骤,更新Program,Startup,nuget packages ...
我正在使用 Syncfusion Essential Studio 2 - 我认为它与此错误无关 - 在许多视图中我声明 variables/creating 对象,例如创建一个 DropDownList 用于在数据网格:
@model CultureMultiModel
@{
var userCultLookupDDL = new Syncfusion.EJ2.DropDowns.DropDownList()
{
FilterType = Syncfusion.EJ2.DropDowns.FilterType.Contains,
Fields = new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings { Text = "Display", Value = "Name", GroupBy = "Parent" }
...
};
var userCultParams = new { @params = userCultLookupDDL };
这在 2.2 中运行良好,但现在我在 new { @params ...
上遇到错误
将鼠标悬停在@params 上时,会显示以下内容:
CS1525:无效的表达式项 'params'
CS1003: 语法错误,',' 预期
我已经尝试了很多事情:
- 使用圆括号@(params) 给出同样的错误
- 添加 nuget 包 Microsoft.AspNetCore.Mvc.Razor.Extensions,同样的错误
- 添加
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
到项目文件,同样的错误 - 更改为@Params 或@arams 会出现错误 CS0103:该名称在当前上下文中不存在
由于在编辑 cshtml 文件时立即 compilation/validation 发生这种情况,我认为这与 Startup 无关,但为了完整起见我还是添加了这个:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<myContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("myConnection"), builder => builder.MigrationsAssembly("myDAL")));
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddLocalization(options => options.ResourcesPath = "Resources");
LocalizationSupport localizationSupport = new LocalizationSupport();
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
options.SupportedCultures = localizationSupport.SupportedCultures;
options.SupportedUICultures = localizationSupport.SupportedCultures;
});
services.AddMemoryCache();
services
.AddMvc()
.AddViewLocalization()
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(SharedModelLocale));
})
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
;
services
.AddControllersWithViews()
.AddNewtonsoftJson(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddRazorPages();
services.Configure<IdentityOptions>(options =>
...
services.AddHttpContextAccessor();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
// << Localization
LocalizationSupport localizationSupport = new LocalizationSupport();
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en"),
SupportedCultures = localizationSupport.SupportedCultures,
SupportedUICultures = localizationSupport.SupportedCultures,
};
app.UseRequestLocalization(options);
// >> Localization
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapAreaControllerRoute(name: "IdentityRoute", areaName: "Identity", pattern: "{area:exists}/{controller=Home}/{action=Index}");
endpoints.MapDefaultControllerRoute();
});
}
和我的项目文件
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Markdig" Version="0.22.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.15" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" PrivateAssets="All" />
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="19.1.0.59" />
<PackageReference Include="System.ServiceModel.Http" Version="4.5.3" />
</ItemGroup>
这可能是一件微不足道的事情,但在任何文档中都没有提到这一点,而且我的搜索也没有返回任何结果。
我已经准备在 JS 中重写 C# 代码,但 Syncfusion 支持提供了正确答案:双重转义。
所以正确的语法是:
var userCultParams = new { @@params = userCultLookupDDL };
虽然这对许多人来说似乎合乎逻辑,但我没有意识到这将是解决方案,甚至没有尝试过。
我确实在其他帖子中读到过它,但无法将其与我的情况相匹配。我的理由是:我已经将关键字 params
转义为 @params
也许其他人 运行 像我一样喜欢这个(并且他们的头靠在墙上)所以我认为分享这个是明智的。