0

I'm trying print a custom attribute in a .cshtml view and I was reading this thread: asp.net mvc custom attributes It's very simple.

My custom attribute

public class TitleAttribute : ActionFilterAttribute
    {
        protected string description;

        public TitleAttribute(String descritionIn)
        {
            this.description = descritionIn;
        }

        public String Description
        {
            get
            {
                return this.description;
            }
        }
    }

But this attribute can be used in different controllers and print this value in a shared layout.

HomeController

[Title("Start Page")]
public ActionResult Index()
{
    return View();
}

RequestController

[Title("This page is releated with request")]
public ActionResult Index()
{
    return View();
}

Is possible print the custom attribute value without use Reflection?

1
  • No. Commented Jan 30, 2015 at 15:00

1 Answer 1

1

If I got your point you'll need to modify your attribute to smth like:

public class TitleAttribute : ActionFilterAttribute
    {
        protected string description;

        public TitleAttribute(String descritionIn)
        {
            this.description = descritionIn;
        }

        public String Description
        {
            get
            {
                return this.description;
            }
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.Controller.ViewBag.Title = description;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

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.