0

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

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

You mean that the BreadCrubAttribute will be extended from System.Attribute and I have to create one more attribute, lets say GetBreadCrumbAttribute which will be extended from ActionFilterAttribute?
Correct. The GetBreadCrumbAttribute would only exist in one place (assuming you can put it on a BaseController class from which all your other controllers descend) and as I mentioned, it would be responsible for putting together the breadcrumbs that it finds and putting them somewhere that the controller and/or the view can retrieve them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.