首页 > 解决方案 > 在 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 文件中?

标签: c#asp.net-mvc

解决方案


string filepath = Server.MapPath(your text file path);
string content = string.Empty;

using (var stream = new StreamReader(filepath)) {
  content = stream.ReadToEnd();
}

ViewBag.context = content;

尝试这个


推荐阅读