什么是阅读从服务器在asp.net MVC的一个文本文件的最佳途径 [英] What is the best way to read in a text file from the server in asp.net-mvc
本文介绍了什么是阅读从服务器在asp.net MVC的一个文本文件的最佳途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我需要的是有一堆在它参考数据的文本文件阅读我的控制器动作之一。现在,我只是把它放在/内容目录。
我的问题是:
- 这是正确的地方,把这个文件或我应该把它放在另一个目录?
- 什么是在asp.net MVC的一个文本文件,是坐在服务器上读取的最佳方法?
解决方案
如果该文件不应该直接可通过网址,你应该把它放在App_Data文件。
有关读书,只需要使用
VAR fileContents = System.IO.File.ReadAllText(使用Server.Mappath(@〜/ App_Data文件/ file.txt的));
In one of my controller actions I need to read in a text file that has a bunch of reference data in it. Right now I simply put it in the "/Content" directory.
My questions are:
- Is this the "right" place to put this file or should I put it in another directory?
- What is the best way to read in a text file in asp.net-mvc that is sitting on the server?
解决方案
If the file should not be directly available via URL, you should put it in App_Data.
For reading it, just use
var fileContents = System.IO.File.ReadAllText(Server.MapPath(@"~/App_Data/file.txt"));
这篇关于什么是阅读从服务器在asp.net MVC的一个文本文件的最佳途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文