I am experiencing a peculiar error after migrating my Asp.Net Core MVC Webapp solution from .Net Core 2.2 to .Net Core 3.1.
I followed the steps described in the official docs, updating Program, Startup, nuget packages ...
I am using Syncfusion Essential Studio 2 - which I believe is not related to this error - and in a number of views I am declaring variables/creating objects, like creating a DropDownList which I use for a lookup in a Datagrid:
@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 };
This was working fine in 2.2 but now I get an error on new { @params ...
When hovering on @params the following is shown:
CS1525: invalid expression term 'params'
CS1003: syntax error, ',' expected
I've tried a number of things:
- using round brackets @(params) gives same error
- adding nuget package Microsoft.AspNetCore.Mvc.Razor.Extensions, same error
- added
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
to project file, same error - changing to @Params or @arams gives error CS0103: the name doesn't exist in the current context
As this happens during the immediate compilation/validation while editing the cshtml file I don't think this is related to Startup but I've added this for completeness anyway:
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();
});
}
and my project file
<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>
It might be something trivial but nothing is mentioned about this in any documentation and my searches haven't returned anything.
I was already preparing to rewrite the C# code in JS but Syncfusion support came through with the correct answer: double escaping.
So the correct syntax is:
Although this might seem logic to many I didn't realize that that would be the solution and didn't even try it.
I did read about it in other posts but just couldn't match it with my situation. My reasoning was: I am already escaping the keyword
params
as@params
Maybe others run into this (and with their head against the wall) like me so I thought it would be wise to share this.