asp.net-mvc-3 - mvc - ActionNameAttribute 从资源获取?

标签 asp.net-mvc-3 resources attributes

嗨,我一直在寻找一些解决方案,但我什么也没找到......

有没有办法通过 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/

相关文章:

asp.net - 在执行大型操作时向 View 发送更新的策略

grails - 如何在 Grails 2.0 中从 war 文件中排除资源?

asp.net-mvc-4 - 本地化比较属性

asp.net - ASP.NET 资源文件中的空格折叠

java - 如何获取 HashMap 值的属性?

html - 如何为mp3文件设置下载属性?

c# - 无法进入 global.asax/Application_Start

css - 打印 8.5x11 纸张格式的 html

asp.net-mvc-3 - mvc3提交模型为空

iPhone:将副本上的资源重命名为 bundle ?