为什么ResourceManager.GetResourceSet在构建后的第一个请求上返回null?(C#)

Col*_*e F 16 c# asp.net resourcemanager nullreferenceexception

我正在研究一个用C#(asp.net)构建的大型Web应用程序.我有一个简单的aspx页面,它为客户端浏览器提供本地化字符串,以便在javascript控件中使用.为了获得字符串,我执行以下操作:

ResourceManager _resources = new ResourceManager(_pathname, typeof(ARM).Assembly);
ResourceSet rs = _resources.GetResourceSet(culture, false, false);

//loop through rs and write the keys & values out to the client in plaintext
Run Code Online (Sandbox Code Playgroud)

这一切都正常,除了在Clean/Build或Rebuild之后立即对页面的第一个请求(如果我只是进行一些更改,然后Build,它工作正常).所以在第一个请求时,当我尝试迭代ResourceSet时,我得到一个空引用异常.但是,如果我在错误之后刷新页面,那么从那时起它就能正常工作.

有谁知道为什么会发生这种情况?

Ale*_*vic 44

方法GetResourceSet的第二个参数"createIfNotExist"必须为true,它告诉ResourceManager加载ResourceSet(如果尚未加载).

ResourceSet rs = _resources.GetResourceSet(culture, true, false);
Run Code Online (Sandbox Code Playgroud)

  • 好的答案,createIfNotExists使我处于错误的状态。(也许Microsoft应该考虑重命名参数名称loadIfNotPresent) (3认同)