【问题标题】:Error declaring verbatim variables after upgrade to asp.net core 3.1升级到 asp.net core 3.1 后声明逐字变量时出错
【发布时间】:2021-08-09 16:35:13
【问题描述】:

将我的 Asp.Net Core MVC Webapp 解决方案从 .Net Core 2.2 迁移到 .Net Core 3.1 后,我遇到了一个特殊错误。
我按照官方docs中描述的步骤,更新Program、Startup、nuget packages...

我正在使用 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 文件时立即编译/验证期间会发生这种情况,我认为这与 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>

这可能是一件微不足道的事情,但在任何文档中都没有提到这一点,而且我的搜索没有返回任何内容。

【问题讨论】:

    标签: c# razor migration asp.net-core-3.1 asp.net-core-2.2


    【解决方案1】:

    我已经准备在 JS 中重写 C# 代码,但 Syncfusion 支持得到了正确答案:双重转义。
    所以正确的语法是:

    var userCultParams = new { @@params = userCultLookupDDL };
    

    虽然这对许多人来说似乎是合乎逻辑的,但我没有意识到这是解决方案,甚至没有尝试过。
    我确实在其他帖子中读过它,但无法与我的情况相匹配。我的理由是:我已经将关键字params 转义为@params

    也许其他人像我一样遇到这种情况(并且他们的头靠在墙上)所以我认为分享这个是明智的。

    【讨论】:

      猜你喜欢
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 2014-04-24
      • 2020-05-25
      相关资源
      最近更新 更多