We help IT Professionals succeed at work.

Call .ashx from silverlight

Hi ,
I am trying to pass string from silverlight page  to .ashx. Is it possible?
xaml code:
========
 Dim uri As String = HtmlPage.Document.DocumentUri.ToString()
        Dim rootUri As String = uri.Remove(uri.LastIndexOf("/"), uri.Length - uri.LastIndexOf("/"))
        Dim diggUrl As String = String.Format(rootUri + "/" + "CTip.ashx?ctURL={0}&ctXML={1}", rURL, xmlStr)
        Dim wc As New WebClient()
        'AddHandler wc.DownloadStringCompleted, AddressOf wc_DownloadStringCompleted
        wc.DownloadStringAsync(New Uri(diggUrl))


Kind regards,
Pooja
Comment
Watch Question

Aaron JabamaniTechnical Architect

Commented:
Yes it is possible to call .ashx file from silverlight.

Just make sure you have proper URL populated from which you are triggering. In my scenario, i have list of grids , each pointing to diff. docs. when clicked it should open in a new window.
Kalpesh ChhatralaSoftware Consultant
CERTIFIED EXPERT

Commented:

Author

Commented:
Hi apeter and Kalpesh2804,
Where is it calling? I need to pass the string variable from xaml to ashx file, then i need to return xml or string from ashx to xaml.
How to call .ashx from silverlight? If i able to call ashx from xaml, inside ProcessRequest, i could call other function which will do certain tasks and likely want to send the response to xaml again...

Kind regards,
Pooja
Aaron JabamaniTechnical Architect

Commented:
Better option for you is to use a WCF service. Add the proxy to the silverlight application. So that you can call the service directly from the silverlight and get the data from the server.

Or you can use a Web service as well.

Author

Commented:
Hi apeter,
Can you explain briefly it to me. Sorry for inconvenience.

Kind regards,
Pooja
Aaron JabamaniTechnical Architect

Commented:
Before going into web service or wcf service i think we do use the .ashx file.

In the below link, you can see the second example accessing the url using "HttpWebRequest" from silverlight. This should work for you to access .ashx file and get the response back from .ashx file. Please let me know if you need any more details.

http://blogs.msdn.com/b/silverlight_sdk/archive/2008/04/01/using-webclient-and-httpwebrequest.aspx

Author

Commented:
Hi  apeter,
This is my code
 Dim uri As String = HtmlPage.Document.DocumentUri.ToString()
        Dim rootUri As String = uri.Remove(uri.LastIndexOf("/"), uri.Length - uri.LastIndexOf("/"))
        Dim diggUrl As String = String.Format(rootUri + "/" + "CTip.ashx?ctURL={0}&ctXML={1}", rURL, xmlStr)
        Dim wc As New WebClient()
       'AddHandler wc.DownloadStringCompleted, AddressOf wc_DownloadStringCompleted
        wc.DownloadStringAsync(New Uri(diggUrl))

It is not going to Ctip.ashx? what should i do here? have to pass string parameters to the handler

Kind regards,
Pooja
Aaron JabamaniTechnical Architect

Commented:
You  are calling the URL is fine...and where is the callback ? something like below. Below event only will give the response.

client.DownloadStringCompleted +=new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);


Hope the url ur generating is fine and the same works in a separage browser.

Author

Commented:
Hi apeter,
It was there , i just commented in the code. It doesn't work that way. Thanks for the time.

Kind regards,
Pooja
Hi apeter,
The code pasted earlier is working fine,
xaml code:
========
 Dim uri As String = HtmlPage.Document.DocumentUri.ToString()
        Dim rootUri As String = uri.Remove(uri.LastIndexOf("/"), uri.Length - uri.LastIndexOf("/"))
        Dim diggUrl As String = String.Format(rootUri + "/" + "CTip.ashx?ctURL={0}&ctXML={1}", rURL, xmlStr)
        Dim wc As New WebClient()
        'AddHandler wc.DownloadStringCompleted, AddressOf wc_DownloadStringCompleted
        wc.DownloadStringAsync(New Uri(diggUrl))

The problem with the code earlier was am getting this exception,
The length of the query string for this request exceeds the configured maxQueryStringLength value.

Solution applied is from this link,
http://social.msdn.microsoft.com/Forums/en/wcf/thread/cb9965c0-162c-4336-99c9-22068d253a6d

So i am closing this question. Thanks for the efforts.

Kind regards,
Pooja
Aaron JabamaniTechnical Architect

Commented:
From when ur getting this error from the initial post itself ?

Author

Commented:
No ,in middle of working . This error is occurred 3 days ago and while researching on it ,I could bring out the solution.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.