I have been able to create ASPX pages without the code behind, but I can't for the life of me figure out the magic combination to get an ASMX page to work without a code behind. Is this even possible?
Thanks
I have been able to create ASPX pages without the code behind, but I can't for the life of me figure out the magic combination to get an ASMX page to work without a code behind. Is this even possible?
Thanks
Quick sample:
<%@ WebService Language="C#" Class="SampleWebService" %>
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SampleWebService : System.Web.Services.WebService
{
[WebMethod]
public string Hello()
{
return "Hello World!";
}
[WebMethod]
public string DoStuff(out string stuff)
{
stuff = "Woohoo!";
return "OK";
}
}
If you want a clean URL without a file extension, you could make something like /PhotoService/Default.aspx. You never include "Default.aspx" in any URL (unless absolutely necessary), so the service URL is simply "/PhotoService/".
<%@ Page Language="C#" %><%--Default.aspx--%>
<%@ Import Namespace="System.Collections.Generic" %>
<script language="C#" runat="server">
private int galleryID;
protected int GalleryID
{
get
{
if( galleryID == 0 && !string.IsNullOrEmpty(Request.QueryString["g"]) )
{
Int32.TryParse( Request.QueryString["g"], out galleryID );
}
return galleryID;
}
}
protected void Page_Load( object sender, EventArgs e )
{
Dictionary<string,string> photos = new Dictionary<string,string>();
photos.Add( "red.jpg", "Red!" );
photos.Add( "grn.jpg", "Green!" );
photos.Add( "blu.jpg", "Blue!" );
// build JSON object list - {{ and }} escape { and }
StringBuilder sb = new StringBuilder( 256 );
foreach( KeyValuePair<string,string> photo in photos )
{
sb.AppendFormat( @",{{photo:""{0}"",caption:""{1}""}}",
string.Format( "/Photos/{0}/{1}", this.GalleryID, photo.Key ),
photo.Value.Replace("\"","\\\"") );
}
// remove first comma
if( sb.Length > 0 ) sb.Remove( 0, 1 );
// insert list into parent object
string output = string.Format( "({{photos:[{0}]}})", sb );
// prepare the response
Response.Clear();
Response.ContentType = "application/json";
// specify size so browser can show progress
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes( output );
Response.AddHeader( "content-length", bytes.Length.ToString() );
// write it
Response.Write( output );
Response.End();
}
</script>
For GalleryID 1, the result would look like:
({photos:[
{image:"/Photos/1/red.jpg",caption:"Red!"},
{image:"/Photos/1/grn.jpg",caption:"Green!"},
{image:"/Photos/1/blu.jpg",caption:"Blue!"}
]})
This can be consumed via jQuery AJAX:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
//<![CDATA[
$.getJSON("/PhotoService/?g=<%=this.GalleryID%>",function(data){
var json=[];
$.each(data.photos,function(i,photo){json.push(photo);});
//json is now an array of photo objects
$(json).each(function(i,photo){
alert(photo.caption);
});
});
//]]>
</script>