C# 如何使保存的html文件呈现而不是显示原始html?
我将html文件保存在Web API应用程序的App_Data文件夹中。伴随客户端(Winforms)应用程序发送一封带有链接的电子邮件,允许用户单击该链接并在浏览器中查看这些html文件,如下所示:C# 如何使保存的html文件呈现而不是显示原始html?,c#,html,asp.net-web-api,html-rendering,webpage-rendering,C#,Html,Asp.net Web Api,Html Rendering,Webpage Rendering,我将html文件保存在Web API应用程序的App_Data文件夹中。伴随客户端(Winforms)应用程序发送一封带有链接的电子邮件,允许用户单击该链接并在浏览器中查看这些html文件,如下所示: // Client Winforms app code internal static bool EmailGeneratedReports(string emailAddr) { bool success = true; try { Microsoft.O
// 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;
}
//客户端Winforms应用程序代码
内部静态bool EmailGeneratedReports(字符串emailAddr)
{
布尔成功=真;
尝试
{
Microsoft.Office.Interop.Outlook.Application app=新的Microsoft.Office.Interop.Outlook.Application();
MailItem MailItem=app.CreateItem(OlItemType.olMailItem);
mailItem.Subject=String.Format(“duckbillP报告生成{0}”,GetYYYYMMDDHHMM());
mailtItem.To=emailAddr;
string rptLinks=string.Format(“,ReportRunnerConstsAndUtils.SERVER_BASE_地址,“鸭嘴兽”,“爷爷”,“201507”);//目前硬编码年份和月份
mailItem.HTMLBody=String.Format(“您的duckbillP报告已附加。您也可以在此处联机查看它们:{0}”,rptLinks);
FileInfo[]rptsToEmail=GetLastReportsGenerated();
foreach(rptsToEmail中的var文件)
{
Add(String.Format(“{0}\\\{1}”,uniqueFolder,file.Name));
}
mailItem.Importance=OlImportance.olImportanceHigh;
mailItem.Display(false);
}
catch(System.Exception sysex)
{
String sysexDetail=String.Format(ReportRunnerConstandUtils.ExceptionFormatString,sysex.Message,
Environment.NewLine、sysex.Source、sysex.StackTrace);
MessageBox.Show(sysexDetail);
成功=错误;
}
回归成功;
}
Web API应用程序中的Get代码运行良好;单击链接时称为:
// 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);
}
//Web API(服务器)控制器代码
[路线(“{unit}/{begindate}”)]
公共字符串Get(字符串单位,字符串开始日期)
{
字符串_单位=单位;
string _begindate=string.Format(“{0}01”,PlatypusWebReportsConstandUtils.hyphenizeyyyyymm(begindate));
字符串appDataFolder=HttpContext.Current.Server.MapPath(“~/App\u Data/”);
string htmlSummaryFilename=string.Format(“platypusSummary_{0}{1}.html”,单位,起始日期);
字符串fullSummaryPath=Path.Combine(appDataFolder,htmlSummaryFilename);
string htmlDetailFilename=string.Format(“platypusDetail_{0}{1}.html”,_unit,_begindate);
字符串fullDetailPath=Path.Combine(appDataFolder,htmlDetailFilename);
字符串summaryHtmlFromFile=File.ReadAllText(fullSummaryPath);
字符串detailHtmlFromFile=File.ReadAllText(fullDetailPath);
返回String.Format(“{0}
{1}”,summaryHtmlFromFile,detailHtmlFromFile);
}
我希望返回的html能够以正常的方式在浏览器中呈现,但它只显示原始html(包括所有的尖括号和html关键字等)。看,如果我在记事本中查看HTML,而不是在浏览器中查看HTML,就会出现这种情况
如何确保GET方法返回的HTML按预期显示
非常清楚地说,我不希望用户看到这样的东西:
<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>
这是标题1(坎达拉)
这是标题2(格鲁吉亚)
这是品目3(塔荷马)
这是一个candara 1.1m的段落
这是一个segoe用户界面段落
这是一个tahoma段落
这是格鲁吉亚1.1em的一段
…但呈现的HTML如下所示:
<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>
您应该告诉浏览器您正在发送HTML内容。像这样的方法应该会奏效:
// 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"
)
};
}
//发送HTTP消息而不是字符串。
公共HttpResponseMessage Get(字符串单元,字符串起始)
{
//在这里处理数据。。。
返回新的HttpResponseMessage()
{
内容=新内容(
String.Format(“{0}
{1}”,summaryHtmlFromFile,detailHtmlFromFile),
Encoding.UTF8,
“文本/html”
)
};
}