【发布时间】:2011-03-23 10:31:25
【问题描述】:
我正在尝试在 mvc 中创建一个自定义属性,以在视图中将其参数用作面包屑。
嗯,这是属性的代码
[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class BreadCrumbAttribute : Attribute {
public BreadCrumbAttribute(string title, string parent, string url) {
this._title = title;
this._parent = parent;
this._url = url;
}
#region named parameters properties
private string _title;
public string Title {
get { return _title; }
}
private string _url;
public string Url {
get { return _url; }
}
private string _parent;
public string Parent {
get { return _parent; }
}
#endregion
#region positional parameters properties
public string Comments { get; set; }
#endregion
}
这是属性的调用
[BreadCrumbAttribute("tile", "parent name", "url")]
public ActionResult Index() {
//code goes here
}
这是我想要获取值的一种方式。 (这是部分视图)
System.Reflection.MemberInfo inf = typeof(ProductsController);
object[] attributes;
attributes = inf.GetCustomAttributes(typeof(BreadCrumbAttribute), false);
foreach (Object attribute in attributes) {
var bca = (BreadCrumbAttribute)attribute;
Response.Write(string.Format("{0}><a href={1}>{2}</a>", bca.Parent, bca.Url, bca.Title));
}
不幸的是,该属性没有以我实现它的方式被调用。虽然,如果我在 Class 中添加属性而不是 Action 方法,它会起作用。 我怎样才能让它工作?
谢谢
【问题讨论】:
标签: asp.net asp.net-mvc custom-attributes