开发者

Accessing controller method from ResultExecutingContext in ASP.NET MVC site

开发者 https://www.devze.com 2023-01-11 00:33 出处:网络
I\'m writing an attribute where I want to access a method on the class that the controller that has the attribute on one of its actions derives from. That\'s quite a mouthful, so let me explain:

I'm writing an attribute where I want to access a method on the class that the controller that has the attribute on one of its actions derives from. That's quite a mouthful, so let me explain:

  • My controller derives from a class that has a method with the following signature: protected bool IsSearchEngine() (the base class itself derives from Controller)
  • One of the actions on my controller has an attribute
  • The attribute derives from ActionFilterAttribute and overloads OnResultExecuting.
  • The OnResultExecuting method has a ResultExecutingContext parameter.

How can I access IsSearchEngine() through the ResultExecutingContext?


UPDATE: So far, I've gotten the compiler and Intellisense to accept this:

(filterContext.Controller as MyAssembly.Controllers.BaseControllerClass).IsSearchEngine()
开发者_Python百科

Is that the right way to do this? I haven't tested it yet.


By casting the resultExecutingContext.Controller to BaseController you can access through all Property and Methods of your base controller, but the protection level of your Method IsSearchEngine() you cannot access it. If you declared your IsSerchEngine() by public you can just call it as ((BaseController)resultExecutingContext.Controller).IsSearchEngine()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号