开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜