开发者

Can an Action Filter have access to a private object in the Controller?

开发者 https://www.devze.com 2023-01-06 20:07 出处:网络
I have public class FundController { private Site _site; public ViewResult Fund() { } } I\'d like to add an Action Filter to 开发者_开发知识库this Fund method:

I have

public class FundController 
{
    private Site _site;
    public ViewResult Fund()
    {
    }
}

I'd like to add an Action Filter to 开发者_开发知识库this Fund method:

public class FundController 
{
    private Site _site;

    [MyFilter]
    public ViewResult Fund()
    {
    }
}

but the Action Filter needs access to _site. Is this possible? If so, how?


Expose the field in a public property, then cast the controller in the filter to FundController.

For example:

FundController controller = (FundController)filterContext.Controller;

Site site = controller.Site;


You could also setup your ActionFilter with a Required Parameter that you then pass in the site

[MyFilter(_site)]
public ViewResult Fund() {
}
0

精彩评论

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