2016-01-04 32 views
1

我有保存在Web API應用程序的App_Data文件夾中的html文件。一個同伴客戶端(的WinForms)應用程序發送一封電子郵件,其中一個鏈接,讓用戶點擊該鏈接,查看這些HTML文件,在瀏覽器中,像這樣:如何獲取我保存的html文件而不是顯示原始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; 
} 

在Web API應用程序的獲取代碼運行良好;這是在點擊鏈接時,什麼叫做:

// 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); 
} 

我希望返回的HTML中以正常的方式瀏覽器將呈現,但不是,它只是顯示了原始的HTML(包括所有的角括號和html關鍵字等)。 IOW,看起來如果我在記事本中查看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> 

...而是呈現的HTML這樣的:

enter image description here

回答

2

你應告訴瀏覽器您正在發送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" 
     ) 
    }; 
} 
相關問題