[英]How can I find the physical path of wwwroot, in a Razor page without a controller?
I need to make a file list in a list, which used to be easy.. but not on Asp.Net Core 2 Razor pages.我需要在一个列表中创建一个文件列表,这在过去很容易......但不是在 Asp.Net Core 2 Razor 页面上。 I cannot find a way to get the physical path of "Poems" which is inside "wwwroot".
我找不到获取“wwwroot”内“Poems”物理路径的方法。 I found many examples using
IHostingEnvironment
but always in a controller.我发现了很多使用
IHostingEnvironment
例子,但总是在控制器中。 But I don't have a controller, don't need one, just the page and code behind..但是我没有控制器,不需要控制器,只有页面和代码后面..
Oh, how I miss WebForms!哦,我多么想念 WebForms!
What is the solution for a Razor Page without a controller?没有控制器的 Razor Page 的解决方案是什么?
Thanks in advance.提前致谢。
You can make it in Razor Page the same way you do it in a controller.您可以像在控制器中那样在 Razor Page 中制作它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Viber.Pages
{
public class TestModel : PageModel
{
private Microsoft.AspNetCore.Hosting.IHostingEnvironment _env;
public TestModel(Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
{
_env = env;
}
public void OnGet()
{
// use the _env here
}
}
}
You can use the contractor of the PageModel to inject the IHostingEnvironment.您可以使用 PageModel 的承包商来注入 IHostingEnvironment。
You can use IWebHostEnvironment class.您可以使用 IWebHostEnvironment 类。 Code block is given below:
代码块如下:
private readonly IWebHostEnvironment _webHostEnvironment;
public HomeController(
IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
}
public IActionResult Index()
{
string rootPath = _webHostEnvironment.WebRootPath
return View();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.