简体   繁体   中英

Load local file (in solution explorer)

I have json file in folder D:\\Projects\\TravelVloggers\\ProjectName.Web\\Helpers\\Folder_1\\myFile.

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. 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

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

    </staticContent>
</system.webServer>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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