我用的是mvc3。是否可以为控制器和操作指定显示名称。
[DisplayName("Facebook Employee")]
public class EmployeeController : Controller
在我的面包屑中,我将获得控制器名称和操作名称
@{
var controllerName = ViewContext.RouteData.Values["Controller"];
var actionName = ViewContext.RouteData.Values["Action"];
}
我希望看到“Facebook 员工”,但它不起作用。
您必须反思控制器类型本身,使用GetCustomAttributes http://msdn.microsoft.com/en-us/library/dwc6ew1d.aspx. Use ViewContext.Controller
获取对控制器本身的引用。像这样的东西:
string controllerName;
Type type = ViewContext.Controller.GetType();
var atts = type.GetCustomAttributes(typeof(DisplayNameAttribute), false);
if (atts.Length > 0)
controllerName = ((DisplayNameAttribute)atts[0]).DisplayName;
else
controllerName = type.Name; // fallback to the type name of the controller
Edit
要对某个操作执行类似操作,您需要首先反思该方法,使用Type.GetMethodInfo
:
string actionName = ViewContext.RouteData.Values["Action"]
MethodInfo method = type.GetMethod(actionName);
var atts = method.GetCustomAttributes(typeof(DisplayNameAttribute), false);
// etc, same as above
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)