繁体   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