2012-06-14 170 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