簡體   English   中英

我嘗試讀取.aspx文件時StreamReader文件路徑錯誤

[英]StreamReader file path error when i try to read .aspx file

我需要根據模板NewsletterTemplate.aspx文件發送電子郵件簡報。

我需要將ArticleID和Language傳遞給NewsletterTemplate.aspx文件,該文件位於“_admin”文件夾下。

由於某種原因,它給我以下錯誤System.ArgumentException: Illegal characters in path. 如果我從URL刪除QueryString部分,那么它不會產生任何錯誤,但我不能提取文章

下面是代碼示例。 在這方面,我將不勝感激

String to, subject, message;
bool isHtml;
isHtml = true;
to = txtEmail.Text;
subject = txtEmailSubject.Text;

ListDictionary replacements = new ListDictionary();
string MessageBody = String.Empty;
string filePath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;

//String TemplatePath = "\_admin\NewsletterTemplate.aspx?ArticleID=" + ddArticleList.SelectedItem.Value.ToString() + "&Language=1";

using (StreamReader sr = new StreamReader(filePath + @"\_admin\NewsletterTemplate.aspx?ArticleID=" + ddArticleList.SelectedItem.Value.ToString() + "&Language=1"))
{
    MessageBody = sr.ReadToEnd();
}
MailDefinition mailDef = new MailDefinition();
MailMessage msgHtml = mailDef.CreateMailMessage(to, replacements, MessageBody, new System.Web.UI.Control());
message = msgHtml.Body.ToString();
//send Email
Helper.SendEmailNewsletter(to, subject, message, isHtml);

StreamReader構造函數需要傳遞文件名,而不是URL地址。 您不能在文件名中包含查詢字符串參數。

如果要傳遞查詢字符串參數,可以使用WebClient類向Webform發送HTTP請求:

using (var client = new WebClient())
{
    MessageBody = client.DownloadString("http://example.com/NewsletterTemplate.aspx?ArticleID=" + HttpUtility.UrlEncode(ddArticleList.SelectedItem.Value.ToString()) + "&Language=1");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM