在 asp.net MVC 中读取文本文件时出错
Error while reading a text file in asp.net MVC
我正在使用 ASP.NET MVC 开发一个网站。我有多行数据,我将其存储在文本行中,并将该文本文件保存在我的应用程序的 ContentData 文件夹中。
我遇到了找不到文件路径的错误
Could not find a part of the path 'C:\Program Files (x86)\IIS
Express\~\ContentData\WTE.txt'
请查看我的代码并帮助我如何执行此操作。
这是视图:
@{
ViewBag.Title = "WhatToExpect";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<h2></h2>
@{
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new
System.IO.StreamReader(@"~/ContentData/WTE.txt");
while((line = file.ReadLine()) != null)
{
string notes= line;
Html.Raw(notes);
counter++;
}
<div id="ChurchImage">
<img src="../../Images1/redeemer.jpg" alt="" />
</div>
file.Close();
}
我试过替换文件路径中的@。没什么 worked.Please 帮帮我
out.Thanks提前
将服务器映射路径添加到您的路径。
System.IO.StreamReader(@Server.MapPath("~/ContentData/WTE.txt"));
// Get your root directoty
var root = AppDomain.CurrentDomain.BaseDirectory;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader( root + @"/ContentData/WTE.txt");
编辑
你的控制器应该是这样的,
public ActionResult Index()
{
var root = AppDomain.CurrentDomain.BaseDirectory;
string line;
System.IO.StreamReader file = new System.IO.StreamReader( root + @"/Content/Site.css");
var fileLines = new List<string>();
while ((line = file.ReadLine()) != null)
{
fileLines.Add(line);
}
// You can use model, temdata or viewbag to carry your data to view.
ViewBag.File = fileLines;
return View();
}
现在您可以在您的视图中使用此 ViewBag.File
来呈现您的文件数据。
@{
string line;
foreach (var item in ViewBag.File)
{
<p>@item</p>
}
}
我正在使用 ASP.NET MVC 开发一个网站。我有多行数据,我将其存储在文本行中,并将该文本文件保存在我的应用程序的 ContentData 文件夹中。 我遇到了找不到文件路径的错误
Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\ContentData\WTE.txt'
请查看我的代码并帮助我如何执行此操作。
这是视图:
@{
ViewBag.Title = "WhatToExpect";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<h2></h2>
@{
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new
System.IO.StreamReader(@"~/ContentData/WTE.txt");
while((line = file.ReadLine()) != null)
{
string notes= line;
Html.Raw(notes);
counter++;
}
<div id="ChurchImage">
<img src="../../Images1/redeemer.jpg" alt="" />
</div>
file.Close();
}
我试过替换文件路径中的@。没什么 worked.Please 帮帮我 out.Thanks提前
将服务器映射路径添加到您的路径。
System.IO.StreamReader(@Server.MapPath("~/ContentData/WTE.txt"));
// Get your root directoty
var root = AppDomain.CurrentDomain.BaseDirectory;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader( root + @"/ContentData/WTE.txt");
编辑
你的控制器应该是这样的,
public ActionResult Index()
{
var root = AppDomain.CurrentDomain.BaseDirectory;
string line;
System.IO.StreamReader file = new System.IO.StreamReader( root + @"/Content/Site.css");
var fileLines = new List<string>();
while ((line = file.ReadLine()) != null)
{
fileLines.Add(line);
}
// You can use model, temdata or viewbag to carry your data to view.
ViewBag.File = fileLines;
return View();
}
现在您可以在您的视图中使用此 ViewBag.File
来呈现您的文件数据。
@{
string line;
foreach (var item in ViewBag.File)
{
<p>@item</p>
}
}