简体   繁体   English

加载本地文件(在解决方案资源管理器中)

[英]Load local file (in solution explorer)

I have json file in folder D:\\Projects\\TravelVloggers\\ProjectName.Web\\Helpers\\Folder_1\\myFile. 我在文件夹D:\\ Projects \\ TravelVloggers \\ ProjectName.Web \\ Helpers \\ Folder_1 \\ myFile中有json文件。

How can I load this file? 如何加载此文件? Later I want to deserialize it. 后来我想反序列化它。

I do not want to put path to local file in my disk. 我不想将本地文件的路径放在磁盘中。

Assuming that you are working in ASP.NET MVC, add your file to Content folder and read with JSON.NET library. 假设您正在ASP.NET MVC中工作,请将文件添加到Content文件夹并使用JSON.NET库读取。 Firstly read the file then deserialize, that's it. 首先读取文件,然后反序列化,就是这样。

using(StreamReader sr = new StreamReader(Server.MapPath("~/Content/file.json")))
{
      yourObject = JsonConvert.DeserializeObject<YourObjectType>(sr.ReadToEnd());
}

Pay attention to add mimetype in web.config 注意在web.config中添加mimetype

<system.webServer>
    <staticContent>
      <mimeMap fileExtension=".json" mimeType="application/json; charset=UTF-8" />

    </staticContent>
</system.webServer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM