簡體   English   中英

升級到 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 后,我遇到了一個特殊錯誤。
我按照官方文檔中描述的步驟,更新程序、啟動、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 package 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>

這可能是一件微不足道的事情,但在任何文檔中都沒有提到這一點,而且我的搜索沒有返回任何內容。

我已經准備在 JS 中重寫 C# 代碼,但 Syncfusion 支持得到了正確答案:雙 escaping。
所以正確的語法是:

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

盡管這對許多人來說似乎是合乎邏輯的,但我沒有意識到這將是解決方案,甚至沒有嘗試過。
我確實在其他帖子中讀過它,但無法與我的情況相匹配。 我的理由是:我已經是 escaping 關鍵字params@params

也許其他人像我一樣遇到了這個(並且他們的頭靠在牆上)所以我認為分享這個是明智的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM