Karthik
Karthik

Reputation: 2399

Execute a aspx page from windows application and get data returned

Hi am working on a windows application in which i have to call a aspx page which read values from database and convert it into an XML.I have no clue how to call a aspx page and make it return a value to the calling windows application.I tried using Web request method it doesnot return anything.Please suggest me a idea how to do this

Upvotes: 1

Views: 1424

Answers (1)

Antonio Bakula
Antonio Bakula

Reputation: 20693

You can use WebClient, something like this :

this is a sitemap XML generated by HttpModule that intercepts requests for XML files:

  WebClient wc = new WebClient();
  string smap = wc.DownloadString("http://www.antoniob.com/sitemap.xml");

And this is a theoretical aspx that returns XML

  WebClient wc = new WebClient();
  string smap = wc.DownloadString("http://www.somesite.com/GetXml.ashx");

There is no difference in the call, except of course in address

On Server side (asp.net app), it would be better to use ASHX handler since is more suited for returning XML, In your ASP.NET application add new item, and choose generic handler :

enter image description here

and here is the code for GetXml.ashx handler that will return sample XML from App_Data folder :

  public class GetXml : IHttpHandler
  {    
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/xml";
      string xml = File.ReadAllText(context.Server.MapPath("~/App_Data/sample.xml"));
      context.Response.Write(xml);
      context.Response.End();    
    }

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }

Upvotes: 1

Related Questions