【发布时间】:2021-10-02 21:12:57
【问题描述】:
这是我在 Startup.cs 中对 NewtonsoftJson 的声明
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.Culture = new CultureInfo(culture);
});
我已经将 Microsoft.AspNetCore.Mvc.NewtonsoftJson 包添加到我的项目中。但是,当我尝试运行它时,我得到下面指向上面代码的异常。我也应该加载 PresentationFramework 吗?我不记得需要为我的其他 API 使用它
System.IO.FileNotFoundException: '无法加载文件或程序集 'PresentationFramework,版本=4.0.0.0,文化=中性, PublicKeyToken=31bf3856ad364e35'。系统找不到文件 指定。'
编辑:
我已经更新了下面的代码。但是,错误仍然出现:
var culture = "en-US";
CultureInfo.CurrentCulture = new CultureInfo(culture, false);
services.AddControllers()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.Culture = new CultureInfo(CultureInfo.CurrentCulture.Name);
});
编辑 2:
我已经注释掉了 AddNewtonSoftJson 部分并保留了 services.AddControllers。这是导致异常的部分。
编辑 3:
这就是我加载应用设置的方式。
var builder = new ConfigurationBuilder()
.SetBasePath(hostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
configuration = builder.Build();
这是堆栈跟踪:
在 System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule 模块, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) 在 System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) 在 System.Reflection.CustomAttribute.FilterCustomAttributeRecord(元数据令牌 caCtorToken, MetadataImport& 范围, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder
1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder1& 属性,RuntimeModule 装饰模块,Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder1 derivedAttributes) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) at System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit) at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit) at System.Attribute.GetCustomAttribute(Assembly element, Type attributeType, Boolean inherit) at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](Assembly element) at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartFactory.GetApplicationPartFactory(Assembly assembly) at Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManager.PopulateDefaultParts(String entryAssemblyName) at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.GetApplicationPartManager(IServiceCollection services) at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCore(IServiceCollection services) at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllersCore(IServiceCollection services) at Microsoft.Extensions.DependencyInjection.MvcServiceCollectionExtensions.AddControllers(IServiceCollection services) at SDR_Filestore.Api.Startup.ConfigureServices(IServiceCollection services) in C:\Users\user1\source\repos\DotNetCore\appo_one\Fileserver.Api\Startup.cs:line 83 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection) at Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder1.c__DisplayClass15_0.g__RunPipeline|0(IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(对象 例如,IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.c__DisplayClass8_0.b__0(IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.StartupLoader.ConfigureServicesDelegateBuilder`1.c__DisplayClass14_0.g__ConfigureServicesWithContainerConfiguration|0(IServiceCollection 服务)在 Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection 服务)
【问题讨论】:
-
你是如何设定文化价值的?
-
在
.AddNewtonsoftJson之外使用new CultureInfo(culture)有效吗? -
我正在从我的配置中获得文化。我已经检查过那里已经有一个值了。
-
我如何在外面设置文化?
-
你是如何加载你的配置的?
标签: c# asp.net-core .net-core