In continuation to my previous post entitled 'Consuming Services in ASP.NET Core Controller' on injecting services in controller, this time I'm writing on how to inject services directly in MVC View. In order to achieve this, a new keyword @inject is used.
Here I'm not writing entire code again as it can be referred from my previous article.
Let's register the service in ConfigureServices method as:
Next is to inject service inside a View as:
Now service is injected and available for use. Let's quickly use it:
Run your application and you will be able to see the required output as:
Hope you enjoyed learning :)
Here I'm not writing entire code again as it can be referred from my previous article.
Let's register the service in ConfigureServices method as:
Next is to inject service inside a View as:
Now service is injected and available for use. Let's quickly use it:
Run your application and you will be able to see the required output as:
Hope you enjoyed learning :)
Comments
Post a Comment