Razorlight: Support to @inject

Created on 22 Aug 2016  路  5Comments  路  Source: toddams/RazorLight

Hi!

I'm trying to use @inject to inject via .netCore DI system some services in my View.
But "@inject" it's not a known tag in RazorLight.

Can you implement this in some future version? Do you want some help to do this?

To now I use a workaround.

Rather than
@inject IServiceUri ServiceUri

I used
@{var ServiceUri = new ServiceUri();}

But... I lost DI :'(

feature-proposal

Most helpful comment

  • Add package

    Install-Package RazorLight.MVC

  • Add RazorLight services in Startup.cs
        public void ConfigureServices(IServiceCollection services)
        {
            ....
            services.AddRazorLight("/Views"); // <- This one
            ....
        }
  • Inject services to your templates
@inject MyProject.TestViewModel myModel

All 5 comments

@inject is a feature of ASP.NET MVC, but I planned to add an integration
Expect this on beta5 release

  • Add package

    Install-Package RazorLight.MVC

  • Add RazorLight services in Startup.cs
        public void ConfigureServices(IServiceCollection services)
        {
            ....
            services.AddRazorLight("/Views"); // <- This one
            ....
        }
  • Inject services to your templates
@inject MyProject.TestViewModel myModel

Hi @toddams Looks like that the MVC package has been depreciated. But I still have the same issue. Would you recommend to use that anyway? Thanks

Hi @toddams I'm using the library in a net standard class library and I wanted to use the inject feature but it doesn't work, any recommendation how I can get that working?

@KishorTiwari @lnodaba

Hello. As i understand framework realization, you cannot use base user scenario for using @inject .

But what you should do:

First way:
Expand your services collection: services.AddRazorLight()
In controller or service constructor get IRazorLightEngine service, and use it for your tasks.

Another way:
You can use example from Tests, method Ensure_Registered_Properties_Are_Injected

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dombarnes picture dombarnes  路  7Comments

WaltDaniels picture WaltDaniels  路  6Comments

tresoldigiorgio picture tresoldigiorgio  路  4Comments

Rizzen picture Rizzen  路  4Comments

ossaltu picture ossaltu  路  6Comments