【发布时间】:2020-06-25 20:40:33
【问题描述】:
我的开发环境是 VS19、ASP .Net Core Razor (non-MVC) 3.1 Pages、Entity Framework Core。
我的模型文件夹中有以下类/模型,它有一个属性:
````
public class FilesObject
{
public string[] Files { get; set; } = new string[1000];
}
在我的 index.cs 文件的 OnGet() 处理程序中,我编写了以下代码以返回一个数组,其中包含指定目录 (wwwroot\documents) 中的文件列表
public class IndexModel : PageModel
{
private readonly ApplicationDbContext _context;
private readonly IWebHostEnvironment _hostingEnvironment;
public IndexModel(ApplicationDbContext context, IWebHostEnvironment hostingEnvironment)
{
_context = context;
_hostingEnvironment = hostingEnvironment;
}
[BindProperty]
public FilesObject FilesObject { get; set; }
public IActionResult OnGet()
{
var webRootPath = _hostingEnvironment.WebRootPath;
var docsPath = Path.Combine(webRootPath, "documents");
FilesObject = Directory.GetFiles(docsPath);
return Page();
}
}
我收到以下错误:错误 CS0029 无法在此行上将类型字符串隐式转换为 Models.FilesObject:
FilesObject = Directory.GetFiles(docsPath);
我最初尝试做这样的事情:
var DocumentInfo = Path.Combine(webRootPath, "documents");
这很好用,但是,我无法将 DocumentInfo 返回到我的 index.cshtml 页面以对其执行任何操作,例如 ForEach 循环。
我明白错误告诉我什么,我只是不知道如何解决它。
任何帮助表示赞赏。提前感谢您的宝贵时间。
【问题讨论】:
-
Path.Combine返回一个string,它只是给定路径的格式良好的字符串。在我看来,您期望目录中的文件列表? -
抱歉,我在上面的源代码中复制了不正确的代码。
标签: c# asp.net asp.net-core razor razor-pages