【问题标题】:Issue importing System.IO.File in .NET Core 3.1 API在 .NET Core 3.1 API 中导入 System.IO.File 时出现问题
【发布时间】:2021-08-10 22:31:46
【问题描述】:

在 .net Core API 中,我有一个带有 HttpPost 端点的数据控制器,该端点从另一个 Web 应用程序接收 json 字符串。 这个 json 字符串可以包含 1 到 5 条记录,由一个 Id、一个描述和一个 base64 图像的字符串组成。 我需要创建一个名为 Data.xml 的 XML 文件,如果它不存在并使用 json 字符串值填充它。 我见过这样的例子:

XDocument document = new XDocument();

if(!File.Exists("MyXmlFile.xml")){
    //Populate with data here if necessary, then save to make sure it exists
    document.Save("MyXmlFile.xml");
}
else{
    //We know it exists so we can load it
    document.load("MyXmlFile.xml");
}

File红色高亮,using语句找不到File命名空间System.IO.File 我还安装了 System.IO.FileSystem NuGet 包,也没有运气。 我曾尝试在 NET Core 3.1 中也有的单独 API 中执行相同操作,但我也无法执行此操作。 有谁知道可能出了什么问题?

控制器代码:

using aspnetcore_api_server.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Xml.Linq;
using System.IO.File;

namespace aspnetcore_api_server.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class DataController : ControllerBase
    {
        [HttpGet]
        public IEnumerable<DataModel> Get()
        {
            return null;
        }

        [HttpPost]
        public List<DataModel> Post([FromBody] string records)
        {
            XDocument dataDocumentStorage = XDocument.Load("Data.xml");

            XDocument document = new XDocument();

            if (!File.Exists("Data.xml"))
            {
                //Populate with data here if necessary, then save to make sure it exists
                document.Save("Data.xml");
            }
            else
            {
                //We know it exists so we can load it
                //document.("Data.xml");
            }

            return null;
        }
    }
}

【问题讨论】:

  • System.IO.File - 您需要 System.IO 的 using 语句或完全限定它
  • 如果我完全设置“使用 System.IO.File”,则找不到文件,在 System.IO 不显示文件作为选项后键入点
  • 我不知道你为什么会遇到这个问题,但我保证这是 .NET Core 中的问题。也许 Visual Studio 提示您生成一个类并且您接受了它,现在您已经定义了自己的 System.IO 命名空间。在您的代码中搜索定义它的文件并将其删除。
  • 会不会是因为它是 API 而不是 Web 应用?我很迷茫,我尝试在另一个我拥有的 api 中做同样的事情并得到相同的结果......文件没有显示出来......我什至安装了包 System.IO.Filesystem 也没有香蕉......跨度>
  • 我已投票决定重新开放。 OP删除了错字,这个问题有一个非常真实,正确的答案。

标签: c# xml api asp.net-core


【解决方案1】:

System.IO.File 当然作为 .NET Core 及更高版本的一部分存在,例如 .NET 5.0。

你遇到的问题是

  1. using System.IO.File 不正确。不能直接在 C# 中的类型上使用 using,除非您使用带有别名的 usingusing static
  2. 您从 ControllerBase 继承,它有一个受保护的 File 方法,而您与之冲突。

将您的实现更改为

using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Xml.Linq;

// here we alias the type System.IO.File to IoFile so it won't conflict
using IoFile = System.IO.File;

namespace aspnetcore_api_server.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class DataController : ControllerBase
    {
        [HttpGet]
        public IEnumerable<DataModel> Get()
        {
            return null;
        }

        [HttpPost]
        public List<DataModel> Post([FromBody] string records)
        {
            XDocument dataDocumentStorage = XDocument.Load("Data.xml");

            XDocument document = new XDocument();

            // here we use that type alias
            if (!IoFile.Exists("Data.xml"))
            {
                //Populate with data here if necessary, then save to make sure it exists
                document.Save("Data.xml");
            }
            else
            {
                //We know it exists so we can load it
                //document.("Data.xml");
            }

            return null;
        }
    }
}

这通过使用别名避免了上述两个问题。

【讨论】:

  • 我认为这里的问题是这个 File.Exists 检查不能在控制器方法中完成,我在一个单独的类中尝试过,它工作得很好。如果我尝试在控制器中执行此操作,则会收到错误消息“ContollerBase.File(byte[], string) 是一种方法,在给定的上下文中无效” - 我无能为力控制器,但不是世界末日
  • 根据您的评论,我已经更新了答案。
猜你喜欢
  • 2021-06-20
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2020-08-29
  • 2021-01-01
  • 2020-09-01
  • 1970-01-01
相关资源
最近更新 更多