How access ViewContext from helper/service, @injected via ViewImports

独自空忆成欢 提交于 2019-12-23 08:29:49

问题


I'm writing helper class and inject in in _ViewImports with

@inject HtmlHelperInject.TestHelper TestHelper

And register in Startup.ConfigureServices with

services.AddTransient<TestHelper>();

How can I obtain ViewContext in this helper class? I tried injecting via controler - not working, via [ViewContext] attribute on property - not working.


回答1:


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);

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 :)

Hope this helps!



来源:https://stackoverflow.com/questions/32892264/how-access-viewcontext-from-helper-service-injected-via-viewimports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!