Direct access GET URLs look like this:
http://localhost/Ajax/AutoCompleteHandler.ashx?Method=GetImage&imageFile=sailbig.jpg
You specify the method to call in the Callback handler along with any of the named parameters each specified as a querystring parameter or POST parameter. Using this approach you can call Callback methods that return binary data types in these formats.
[CallbackMethod(ContentType="image/png")] public Stream GetStockHistoryGraph(string symbol) { StockServer server = new StockServer(); byte[] img = server.GetStockHistoryGraph(new[] { symbol }, "History for " + symbol.ToUpper(), 450, 300, 2); MemoryStream ms = new MemoryStream(img); return ms; }
[CallbackMethod(ContentType="image/png")] public byte[] GetStockHistoryGraph(string symbol) { StockServer server = new StockServer(); byte[] img = server.GetStockHistoryGraph(new[] { symbol }, "History for " + symbol.ToUpper(), 450, 300, 2); return img; }
[CallbackMethod(ContentType="image/png")] public Bitmap GetImage(string imageFile) { Bitmap img = new Bitmap(HttpContext.Current.Server.MapPath("~/images/" + imageFile)); return img; }
Note that you can set the content type in the attribute which is recommended. If not provided Callback Handler will use the Bitmap's internal ImageFormat (RawFormat) if set properly (typically when you load from file on disk).
[CallbackMethod(ReturnAsRawString=true,ContentType="text/plain; charset=UTF-8")] public string GetCities() { var searchFor = HttpContext.Current.Request.QueryString["q"]; busCustomer customer = new busCustomer(); var res = (from cust in customer.Context.nw_Customers where cust.City.Contains(searchFor) orderby cust.City select new { City = cust.City, cust.CustomerID }).Distinct(); StringBuilder sb = new StringBuilder(); foreach (var city in res) { sb.AppendLine(city.City + "|" + city.CustomerID); } return sb.ToString(); }
Content type is optional. The default content type for raw string results if not specified is text/html (default Response.ContentType). You can also override the content type in code with HttpContext.Current.Response.ContentType.
Additionally you can also specify a querystring or POST parameter of ResultFormat=string to force string output as raw output rather than JSON encoded output in which case the ReturnAsRawString parameter is not required on the CallbackMethod attribute.