开发者

Asp.Net resources confusion

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 开发者_StackOverflow社区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.


try below links

http://haacked.com/archive/2009/12/12/localizing-aspnetmvc-validation.aspx

MVC Data annotation in different languages?

if none works for you then let us know!!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜