【问题标题】:Create Model To Hold Results of Path.Combine Array创建模型以保存 Path.Combine 数组的结果
【发布时间】: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


【解决方案1】:

要获取目录中的文件列表,请执行以下操作:

DirectoryInfo d = new DirectoryInfo(@"path/that/you/are/interested/in");
FileInfo[] Files = d.GetFiles("*.txt"); // e.g. getting Text files

DirectoryInfoFileInfo

【讨论】:

  • 我上面的源代码不正确,已经更正了。
【解决方案2】:

错误 CS0029 无法在此行上将类型字符串隐式转换为 Models.FilesObject:

Directory.GetFiles 返回string[],所以你需要像下面这样改变:

FilesObject.Files = Directory.GetFiles(docsPath);

在使用它之前,您需要初始化FilesObject。这是一个如下所示的工作演示:

1.Index.cshtml:

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

@foreach(var data in Model.FilesObject.Files)
{
    <div>
        @data;
    </div>

}

2.Index.cshtml.cs:

public class IndexModel : PageModel
{
    private readonly IWebHostEnvironment _hostingEnvironment;

    public IndexModel( IWebHostEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    [BindProperty]
    public FilesObject FilesObject { get; set; }

    public IActionResult OnGet()
    {
        FilesObject = new FilesObject() { };//need to add this line to initialize the model
        var webRootPath = _hostingEnvironment.WebRootPath;
        var docsPath = Path.Combine(webRootPath, "documents");
        FilesObject.Files = Directory.GetFiles(docsPath);

        return Page();
    }
}

结果:

【讨论】:

  • 嗨@1Zero3Tech,如果我的回答对你有帮助,你能接受吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
相关资源
最近更新 更多