简体   繁体   中英

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

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM