Asp.net Mvc:列出具有特定属性的控制器上的所有操作
我试图列出所有控制器及其具有特定属性的操作,以创建动态谷歌站点地图。这样我就可以用属性“标记”某些操作,以便它们显示在站点地图中。
在这里我找到了如何获取所有控制器。但我不确定如何获取具有特定属性的所有操作。我尝试了 GetMethods,然后使用 GetCustomAttributes,但我不确定这是否是正确的方法。感觉有点过于复杂了。
一旦我获得了控制器及其操作,我将使用此处 获取网址。您可能会注意到埃里克·邓肯(Eric Duncan)谈到了我在这个问题中试图完成的任务。
提前致谢。
I am trying to list all the controllers and their actions with specific attributes to create a dynamic google sitemap. So that I can "mark" certain actions with an attribute so they show up in the sitemap.
Here I found out how to get all the controllers. But I am not sure how to get all their Actions with a particular attribute. I tried GetMethods and then use GetCustomAttributes but I am not sure if that's the right way to do it. It felt a little over complicated.
Once I get the controllers and their actions I was going to use the technique explained here to get the urls. As you may notice Eric Duncan talks about what I am trying to accomplish in that question.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在单元测试中使用一些代码来验证某些操作是否用属性修饰。它使用反射和一些可枚举扩展方法的优点。我认为你可以适应这一点。请注意,如果您只关心它是否存在,则可以在枚举上使用 Count() 而不是获取实际属性。通过这种方式,您可以灵活地使用属性来自定义行为。使用继承树可以让您装饰整个控制器。
I use some code in my unit tests to verify that certain actions are decorated with attributes. It uses reflection with some enumerable extension method goodness. I think you could adapt this. Note, if you only care about whether it exists or not, you could use Count() on the enumeration rather than getting the actual attribute. This way allows you some flexibility in using attribute properties to customize the behavior. Using the inheritance tree would allow you to decorate an entire controller.