asp.net mvc 自定义属性

发布于 2024-10-25 05:45:19 字数 1465 浏览 4 评论 0原文

我正在尝试在 mvc 中创建一个自定义属性,以在视图中将其参数用作 breadCrumb。

好吧,这是属性的代码,

[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));
}    

不幸的是,该属性没有按照我实现它的方式被调用。不过,如果我在类中添加属性而不是操作方法,它就会起作用。 我怎样才能让它发挥作用?

谢谢

I am trying to create a custom attribute in mvc to use it's parameters in a view as breadCrumb.

well, this is the code of the attribute

[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

}

this is the call of the attribute

[BreadCrumbAttribute("tile", "parent name", "url")]
    public ActionResult Index() {
     //code goes here
     }

this is a way of how I'd like to get the values. (this is a partial view)

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));
}    

Unfortunately, the attribute didn't get call with the way I implement it. Although, If I add the attribute in Class instead of an Action method it worked.
How could I make it work?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┾廆蒐ゝ 2024-11-01 05:45:19

问题是您正在使用反射来获取类的属性,因此它自然不包括在操作方法上定义的属性。

要获得这些,您应该定义一个 ActionFilterAttribute,并在 OnActionExecuting 或 OnActionExecuted 方法中,您可以使用 filterContext.ActionDescriptor.GetCustomAttributes() 方法 (此处的 MSDN 说明)。

请注意,使用此解决方案,您可能会拥有两种不同类型的属性:第一个是您编写的属性,用于定义面包屑。第二个是查看执行操作的属性并构建面包屑(并且可能将其添加到 ViewModel 或将其粘贴到 HttpContext.Items 或其他内容中)。

The problem is that you are using reflection to get the attributes for the class, so naturally it does not include attributes defined on the action method.

To get those, you should define an ActionFilterAttribute, and in the OnActionExecuting or OnActionExecuted method, you can use filterContext.ActionDescriptor.GetCustomAttributes() method (MSDN description here).

Note that with this solution, you will likely have two different types of attributes: The first one is the one you wrote, to define the breadcrumbs. The second is the one that looks at the attributes on the executing action and builds up the breadcrumb (and presumably adds it to the ViewModel or sticks it in HttpContext.Items or something).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文