Learning
Learning

Reputation: 20011

StreamReader file path error when i try to read .aspx file

I need to send an email newsletter based on a template NewsletterTemplate.aspx file.

I need to pass ArticleID and Language to the NewsletterTemplate.aspx file this file in under folder "_admin".

For some reason it gives me following error System.ArgumentException: Illegal characters in path. and if i remove the QueryString part from the URLthen it doesn't generate any error but i cant extract the Article

Below is teh code example. I would appreciate help in this regard

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

Upvotes: 0

Views: 967

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

The StreamReader constructor expects to be passed a filename, not an URL address. You cannot have query string parameters in a filename.

If you want to pass query string parameters you could send an HTTP request to the webform using the WebClient class:

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

Upvotes: 2

Related Questions