如何解决升级到 asp.net core 3.1 后声明逐字变量时出错
在将我的 Asp.Net Core MVC Webapp 解决方案从 .Net Core 2.2 迁移到 .Net Core 3.1 后,我遇到了一个奇怪的错误。
我按照官方 docs 中描述的步骤,更新程序、启动、nuget 包...
我正在使用 Syncfusion Essential Studio 2 - 我认为这与此错误无关 - 在许多视图中,我正在声明变量/创建对象,例如创建用于在 Datagrid 中查找的 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:无效的表达式术语“参数”
CS1003: 语法错误,',' 预期
我尝试了很多东西:
- 使用圆括号@(params) 给出同样的错误
- 添加 nuget 包 Microsoft.AspNetCore.Mvc.Razor.Extensions,同样的错误
- 在项目文件中添加了
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
,同样的错误 - 更改为@Params 或@arams 会出现错误 CS0103:当前上下文中不存在该名称
由于在编辑 cshtml 文件时立即编译/验证期间发生这种情况,我认为这与启动无关,但为了完整性我还是添加了它:
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>
这可能是一件微不足道的事情,但在任何文档中都没有提及此事,我的搜索也没有返回任何内容。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。