2012-06-14 125 views
2

嗨,我一直在寻找一些解决方案,但我什么也没找到...mvc - ActionNameAttribute从资源获取?

有没有办法使用ActionNameAttribute资源?

例如与DisplayNameAttribute的特性,我们可以诉诸:

[Display(Name = "labelForName", ResourceType = typeof(Resources.Resources))] 
    public string name{ get; set; } 

但我不知道的方式利用资源为我的操作方法......

Thanks¡

回答

3

你可以编写自定义属性来实现此任务:

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=true)] 
public sealed class LocalizedActionNameAttribute : ActionNameSelectorAttribute 
{ 
    public LocalizedActionNameAttribute(string name, Type resourceType) 
    { 
     Name = name; 
     ResourceType = resourceType; 
    } 

    public Type ResourceType { get; private set; } 
    public string Name { get; private set; } 

    public override bool IsValidName(ControllerContext controllerContext, string actionName, System.Reflection.MethodInfo methodInfo) 
    { 
     var rm = new ResourceManager(ResourceType); 
     var name = rm.GetString(Name); 
     return string.Equals(actionName, name, StringComparison.OrdinalIgnoreCase); 
    } 
} 

和那么:

[LocalizedActionName("Index", typeof(Resources.Resources))] 
public ActionResult Index() 
{ 
    return View(); 
} 
+0

它工作完美谢谢 – mcartur