c# - 在 C# 上渲染 html 时删除双引号
问题描述
public ActionResult ReadConfig()
{
IEnumerable<string> text = System.IO.File.ReadLines(@"C:\Users\nys\source\repos\WebApplication4\WebApplication4\Configs\Json_test.txt");
IEnumerator<string> en = text.GetEnumerator();
var X = @"<p>";
List<string> T = new List<string>();
while (en.MoveNext())
{
X += en.Current;
T.Add(en.Current);
T.Add("\n");
}
X += "</p>";
var dat = new data { context = X };
ViewBag.context = T;
return View(dat);
}
因此,这里的代码应该从文件中读取,并将其作为文本输出到 html 页面中。
@model WebApplication4.Models.data
@{
ViewBag.Title = "ReadConfig";
}
<h2>ReadConfig</h2>
<div>
@foreach(string a in ViewBag.context)
{
@Html.Raw(a)
<br />
}
</div>
html 不会打印出格式化的文本,但我看到双引号内的文本。
如何保留文本文件中文本的间距和格式并将其简单地吐出到 html 文件中?
解决方案
string filepath = Server.MapPath(your text file path);
string content = string.Empty;
using (var stream = new StreamReader(filepath)) {
content = stream.ReadToEnd();
}
ViewBag.context = content;
尝试这个
推荐阅读
- github - github会有多少用户限制搜索?
- google-apps-script - 如何在 Google 表格 onEdit 触发器中获取范围
- javascript - “从不”类型上不存在属性“拆分”
- node.js - 遵循指南时名称仅在大小写上有所不同的多个模块
- java - 试图延迟java中树节点之间的绘画过程
- google-cloud-platform - Slack webhook 和模型训练任务
- javascript - 指定路线上的传单标记移动
- c - 安装从 void* 到 int 的 Nachos 3.4 错误会失去精度
- c++ - C++标准输出流(cout)中无法输出按位运算的结果
- excel - 如何在 WorksheetFunction.CountA 中放置工作表引用?