我正在使用ASP.NET MVC开发网站。我将多行数据存储在文本行中,并将该文本文件保存在应用程序的ContentData文件夹中。我遇到这样的错误:找不到文件路径
找不到路径“ 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();
}
我尝试替换文件路径中的@。没有任何效果,请帮帮我,谢谢
// 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>
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句