简体   繁体   English

如何获取保存的html文件而不显示原始html?

[英]How can I get my saved html file to be rendered rather than displaying the raw html?

I have html files saved in the App_Data folder of a Web API app. 我将HTML文件保存在Web API应用程序的App_Data文件夹中。 A companion client (Winforms) app sends an email with a link that allows the user to click that link and view these html files in their browser like so: 配套客户端(Winforms)应用程序发送带有链接的电子邮件,该链接允许用户单击该链接并在其浏览器中查看这些html文件,如下所示:

// Client Winforms app code
internal static bool EmailGeneratedReports(string emailAddr)
{
    bool success = true;
    try
    {
        Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
        MailItem mailItem = app.CreateItem(OlItemType.olMailItem);
        mailItem.Subject = String.Format("duckbillP Reports generated {0}", GetYYYYMMDDHHMM());
        mailItem.To = emailAddr;
        string rptLinks = String.Format("<a href=\"{0}/api/{1}/{2}/{3}\" target=\"_blank\">Price Compliance Report Online</a>", ReportRunnerConstsAndUtils.SERVER_BASE_ADDRESS, "Platypus", "gramps", "201507"); // hardcoding year and month for now
        mailItem.HTMLBody = String.Format("<html><body><p>Your duckbillP reports are attached. You can also view them online here:</p>{0}</body></html>", rptLinks);
        FileInfo[] rptsToEmail = GetLastReportsGenerated();
        foreach (var file in rptsToEmail)
        {
            mailItem.Attachments.Add(String.Format("{0}\\{1}", uniqueFolder, file.Name));                  
        }
        mailItem.Importance = OlImportance.olImportanceHigh;
        mailItem.Display(false);
    }
    catch (System.Exception sysex)
    {
        String sysexDetail = String.Format(ReportRunnerConstsAndUtils.ExceptionFormatString, sysex.Message,
            Environment.NewLine, sysex.Source, sysex.StackTrace);
        MessageBox.Show(sysexDetail);
        success = false;
    }
    return success;
}

The Get code in the Web API app runs just fine; Web API应用程序中的Get代码运行良好。 this is what is called when the link is clicked: 当单击链接时,这就是所谓的:

// Web API (server) Controller code
[Route("{unit}/{begindate}")]
public string Get(string unit, string begindate)
{
    string _unit = unit;
    string _begindate = String.Format("{0}01", PlatypusWebReportsConstsAndUtils.HyphenizeYYYYMM(begindate));
    string appDataFolder = HttpContext.Current.Server.MapPath("~/App_Data/");

    string htmlSummaryFilename = string.Format("platypusSummary_{0}_{1}.html", _unit, _begindate);
    string fullSummaryPath = Path.Combine(appDataFolder, htmlSummaryFilename);

    string htmlDetailFilename = string.Format("platypusDetail_{0}_{1}.html", _unit, _begindate);
    string fullDetailPath = Path.Combine(appDataFolder, htmlDetailFilename);

    String summaryHtmlFromFile = File.ReadAllText(fullSummaryPath);
    String detailHtmlFromFile = File.ReadAllText(fullDetailPath);
    return String.Format("{0}<br/><br/>{1}", summaryHtmlFromFile, detailHtmlFromFile);
}

I was hoping that the html returned would render in the browser in the normal way, but instead of that it just displays the raw html (including all the angle brackets and html keywords, etc.). 我希望返回的html将以正常方式在浏览器中呈现,但它只是显示原始html(包括所有尖括号和html关键字等),而不是它。 IOW, it appears if I'm viewing HTML in Notepad, rather than in a browser. IOW,如果我在记事本中而不是在浏览器中查看HTML,就会出现。

How can I see to it that the HTML returned by the GET method displays as intended? 我如何看待GET方法返回的HTML是否按预期显示?

To be perfectly clear, I don't want the user to see stuff like this: 明确地说,我不希望用户看到以下内容:

<h1>This is heading 1 (Candara)</h1>
<h2>This is heading 2 (Georgia)</h2>
<h3>This is heading 3 (Tahoma)</h3>
<p class="candara">This is a Candara 1.1em paragraph</p>
<p class="segoe">This is a Segoe UI paragraph</p>
<p class="tahoma">This is a Tahoma paragraph</p>
<p class="georgia">This is a Georgia 1.1em paragraph</p>

...but rather the rendered HTML like this: ...但是渲染的HTML像这样:

在此处输入图片说明

You should tell the browser that you're sending HTML content. 您应该告诉浏览器您正在发送HTML内容。 Something like this should work: 这样的事情应该起作用:

// Send an HTTP message instead of a string.
public HttpResponseMessage Get(string unit, string begindate)
{
    // Here you process the data...

    return new HttpResponseMessage()
    {
        Content = new StringContent(
            String.Format("{0}<br/><br/>{1}", summaryHtmlFromFile, detailHtmlFromFile), 
            Encoding.UTF8, 
            "text/html"
        )
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM