嗨,我一直在寻找一些解决方案,但我什么也没找到......
有没有办法通过 ActionNameAttribute 使用资源?
例如,使用属性中的 DisplayNameAttribute,我们可以求助于:
[Display(Name = "labelForName", ResourceType = typeof(Resources.Resources))]
public string name{ get; set; }
但我不知道如何为我的操作方法使用资源......
谢谢...
最佳答案
您可以编写自定义属性来完成此任务:
[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();
}
关于asp.net-mvc-3 - mvc - ActionNameAttribute 从资源获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11038308/