C# – Passing parameters from silverlight to ASP.net


I've made a little game in silverlight that records users scores whilst they play.

I decided it would be a lot better if I could implement a leaderboard, so I created a database in mySQL to store all the high scores along with names and dates. I have created some communications to the database in ASP.net. This works and I can simply insert and get data within the code.

It's now time to link the silverlight project with the ASP.net database communications, so I can send the users name and score as variables to my ASP.net code and then it will upload it to the database. That's all I need. Surely there must be an easy way of doing this, I just can't seem to find any ways when researching.

Thanks in advance,

Best Solution

At first you need add Generic Handler to your ASP.Net project.

  public class Handler1 : IHttpHandler
    public void ProcessRequest(HttpContext context)
        string userName = context.Request["user"];
        int score = int.Parse(context.Request["score"]);
        //And store it in DB

After you need call this handler from SilverLight app:

         string uri = HtmlPage.Document.DocumentUri.ToString();

        // Remove the web page from the current URI to get the root URI. 
         string   rootUri = uri.Remove(uri.LastIndexOf('/'),
         uri.Length - uri.LastIndexOf('/')); 

         string diggUrl = String.Format(rootUri + "/" + "test.ashx?user={0}&score={1}", "testuser", "234");

        // Initiate Async Network call to Digg
        WebClient diggService = new WebClient();
        diggService.DownloadStringAsync(new Uri(diggUrl));