您可以编写自定义属性来完成此任务:
[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();
}