I have a blazor component in my application:
public class IndexComponent : ComponentBase
{
public string ContentRoot { get; set; }
public string WebRoot { get; set; }
private IHostingEnvironment HostingEnvironment;
public IndexComponent(IHostingEnvironment hostingEnvironment)
{
HostingEnvironment = hostingEnvironment;
}
protected override async Task OnInitAsync()
{
//Some Code Here
}
}
I am trying to use DI in my app , for example IHostingEnvironment.
Code give no compile time error here but when i run it than in code behind file of this razor (Index.razor.g.cs file):
public class Index : IndexComponent
at this line it says:
There is no argument given that corresponds to the required formal parameter hostingEnvironment of IndexComponent.IndexComponent
This can be solved by using @inject IHostingEnvironment in Razor file but I am moving my function block from Razor to IndexComponent.cs file so need it there.
Neither of it works in below way:
[Inject]
IHostingEnvironment HostingEnvironment
What will work here?
Note: No use of ViewModel
Update 1
In StartUp.cs by adding namespace
using Microsoft.AspNetCore.Hosting.Internal;
And than
services.AddSingleton<IHostingEnvironment>(new HostingEnvironment());
It is now able to register IHostingEnvironment on client side project but it does not have values for its properties (contentrootpath and webrootpath).
Only one thing is available here which is EnvironmentName and its value is always Production ,