在ASP.NET Core中,可以通过以下步骤在Razor视图中注入IHostingEnvironment:
public void ConfigureServices(IServiceCollection services)
{
// 其他代码...
services.AddSingleton(provider => provider.GetService());
// 其他代码...
}
@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment HostingEnvironment
@HostingEnvironment.ContentRootPath
通过以上步骤,就可以在Razor视图中注入和使用IHostingEnvironment来访问应用程序的环境信息了。