Justin Soliz
Justin Soliz

Reputation: 2811

Asp.Net Mvc - Find attributes for a given method

I'm trying to write some extensions for testing and one of the specs I want to run would be based on finding the list of attributes associated with a method (action).

So far, this is the code I use to grab attributes off a method:

var homeController = new HomeController();
(controller.GetType().GetMethod(action, parameters)).GetCustomAttributes(false)

etc...

or for a controller, something like this (which is more explicit and not reusable)

(typeof(HomeController)).GetCustomAttributes(false)

So the question is, given I have a generic action of type ActionResult example:

var actionResult = new HomeController().Index();

Is there a way to find the attributes of that instance of the actionResult ?

Upvotes: 1

Views: 687

Answers (1)

marcind
marcind

Reputation: 53181

If you just have actionResult then you don't really have the action method. You just have the result of its execution. Multiple methods can return the same result, for example.

You have to have a reference to the method itself. Your reflection calls should be fine. What do you not like about them?

Upvotes: 2

Related Questions