【发布时间】:2021-10-30 07:36:37
【问题描述】:
我有一个 Asp.Net Core RazorPages 应用程序,并且正在尝试注入 WebHosting 环境,以便我可以访问文件。我在解决方案文件夹下有一个名为“报告”的文件夹。我以这种方式在我的 RazorPage 代码中注入 IWebHostEnvironment:
public class IndexModel : PageModel {
private readonly Microsoft.AspNetCore.Hosting.IWebHostEnvironment webHostEnvironment1;
public IndexModel(Microsoft.AspNetCore.Hosting.IWebHostEnvironment webHostEnvironment)
{
this.webHostEnvironment1 = webHostEnvironment;
}
public void OnGet() {
string reportPath = System.IO.Path.Combine(webHostEnvironment1.ContentRootPath, "Reports", "TestReport.repx");
Console.WriteLine("CONTENTRootPath: " + webHostEnvironment1.ContentRootPath);
Console.WriteLine("WEBRootPath: " + webHostEnvironment1.WebRootPath);
Console.WriteLine("ReportPath: " + reportPath);
}
我的 Startup.cs:
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
Configuration = configuration;
WebHostEnvironment = environment;
}
public IConfiguration Configuration { get; }
public IWebHostEnvironment WebHostEnvironment { get; }
当我运行我的应用程序时,它没有抛出任何错误,但它没有写入控制台、contentrootpath 和 reportfilepath。我的代码正确吗?
【问题讨论】:
-
如果我的回复有帮助,请采纳为答案(点击回复旁边的标记选项将其从灰色切换为填写。),请参阅meta.stackexchange.com/questions/5234/…
标签: asp.net-core dependency-injection