简体   繁体   中英

How to use injected service from razor layout view in child view

Just learning about injected services in asp.net core razor views. But I haven't been able to find out how to use a service that is injected in the layout view (will be used by many views), in child views.

Maybe something conceptually like this:

Layout View

@inject IOptions<PortalConfiguration> OptionsPortalConfiguration

<some HTLM markup>
@RenderBody(OptionsPortalConfiguration) <--pass it here???

Child View

Not sure how to reference it.

Another approach for that kind of situations would be to create static helper methods, which can be called in all views without injection.

public static class TestHelperExtensions
{
    public static ITestProvider TestProvider { get; set; }

    public static string Get(this ITestHelper html, string arg)
    {
        return TestProvider.Get(arg);
    }
}

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