Asp.Net资源混乱

发布于 2024-11-27 17:18:26 字数 1248 浏览 3 评论 0原文

因此,我尝试在一个简单的 Net 3.5 / Mvc2 站点中使用本地和全局资源。

首先,我尝试在全局资源中设置验证消息,但随后出现了第一个问题:在 DataAnnotations 属性上设置全局资源没有给出错误,但在运行时它只是不会从中恢复字符串(运行时也没有错误) )。示例代码:

[Required(
        ErrorMessageResourceType = typeof(Resources.Global),
        ErrorMessageResourceName = "LastNameRequired")]
    public string LastName { get; set; }

全局资源无法设置为公共(我认为这就是问题所在),因此我想将消息移动到本地资源中的 DataAnnotations 中使用:

[Required(
        ErrorMessageResourceType = typeof(Views.MyApp.App_LocalResources.Apply_aspx),
        ErrorMessageResourceName = "LastNameRequired")]
    public string LastName { get; set; }

这给我带来了 2 个替代问题:如果我将本地资源设置为构建操作 =使用 GetLocalResourceObject 方法从视图中正确读取资源的内容,但在尝试从 DataAnnotations 读取它们时,我得到以下内容:

找不到任何适合指定文化或中立文化的资源。确保“MyNamespace.Website.Mvc.Views.MyApp.App_LocalResources.Apply.aspx.resources”在编译时正确嵌入或链接到程序集“MyNamespace.Website.Mvc”中,或者所需的所有附属程序集都是可加载且完全的已签署。

然后我尝试设置构建操作=嵌入资源。这样,App_LocalResources 文件夹就不会发布,并且在尝试使用 GetLocalResouceObject 从视图中读取本地资源时出现以下错误:

找不到该页面的资源类。请检查资源文件是否存在,然后重试。

我显然试图在谷歌上寻找解决方案,但我找不到任何与我的场景类似的东西,所以我在这里问。

我必须做什么才能让一切顺利?

谢谢。

So I'm trying to use both local and global resources in a simple Net 3.5 / Mvc2 site.

First of all I tried setting my validation messages in global resources but then the first problem arose: setting the global resource on the DataAnnotations attribute gives no errors, but at runtime it just doesn't recover the strings from it (no errors at runtime either). Sample code:

[Required(
        ErrorMessageResourceType = typeof(Resources.Global),
        ErrorMessageResourceName = "LastNameRequired")]
    public string LastName { get; set; }

Global resources cannot be set to public (I think that's the problem) so I thought to move messages to be used in DataAnnotations in local resources:

[Required(
        ErrorMessageResourceType = typeof(Views.MyApp.App_LocalResources.Apply_aspx),
        ErrorMessageResourceName = "LastNameRequired")]
    public string LastName { get; set; }

That brought me to 2 alternate problems: if I set the local resource to build action = content the resources are read correctly from the view using the GetLocalResourceObject method, but I get the following when trying to read them from DataAnnotations:

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "MyNamespace.Website.Mvc.Views.MyApp.App_LocalResources.Apply.aspx.resources" was correctly embedded or linked into assembly "MyNamespace.Website.Mvc" at compile time, or that all the satellite assemblies required are loadable and fully signed.

Then I tried to set the the build action = embedded resource. This way the App_LocalResources folder doesn't get published, and I get the following error when trying to read local resources from the view with GetLocalResouceObject:

The resource class for this page was not found. Please check if the resource file exists and try again.

I obviously tried to google around for a solution but I couldn't find anything that resembles my scenario, so I'm asking here.

What do I have to do to make evertything smooth?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2024-12-04 17:18:26

我自己进一步挖掘找到了一种方法。

我试图使用代码中 Views 文件夹中声明的“LocalResources”。这就是问题所在。

正确的做法是仅将这些资源文件用于视图。然后,对于代码,在根级别声明一个 App_LocalResources 文件夹,并将资源文件放在那里,并将它们设置为公共。

那行得通。

I found a way by myself digging further.

I was trying to use the "LocalResources" declared in the Views folder from the code. That was the problem.

The right course of action is using those resources files only for the views. Then, for the code, declared a App_LocalResources folder at root level, and put resources files there, setting them to public.

That worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文