从助手/服务如何访问ViewContext,通过ViewImports @injected [英] How access ViewContext from helper/service, @injected via ViewImports
问题描述
我写的辅助类,以
@inject HtmlHelperInject.TestHelper TestHelper
而在 Startup.ConfigureServices
与
services.AddTransient<TestHelper>();
我怎样才能获得 ViewContext
这个辅助类?通过对房地产 [ViewContext]
属性不能正常工作, - - 我试图通过CONTROLER注射。不工作
How can I obtain ViewContext
in this helper class? I tried injecting via controler - not working, via [ViewContext]
attribute on property - not working.
推荐答案
截至目前(beta8)做到这一点的方式是贯彻落实...等待它... ICanHasViewContext
。这个接口增加了以下合约:
As of right now (beta8) the way to do this is to implement... wait for it... ICanHasViewContext
. This interface adds the following contract:
void Contextualize(ViewContext viewContext);
当注入您的自定义工具MVC电话情境化
并传递当前 ViewContext
。注:在将来的版本中这个机制很可能会改变。如果不是,这个名字势必:)
When injecting your custom utility MVC calls Contextualize
and passes in the current ViewContext
. Note: this mechanism will most likely change in future releases. If not, the name certainly will :)
希望这有助于!
这篇关于从助手/服务如何访问ViewContext,通过ViewImports @injected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!