Passing parameters from silverlight to ASP.net

后端 未结 5 658
执念已碎
执念已碎 2021-01-16 16:58

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 creat

相关标签:
5条回答
  • 2021-01-16 17:05

    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));
    
    0 讨论(0)
  • 2021-01-16 17:13

    An easy way to do this is to have your Silverlight code create a REST URL by encoding the information into the query string, and invoking an .aspx page on the server. The page wouldn't need to return any markup; it would just handle the back-end stuff and return.

    Alternatively, you could make a web service call from Silverlight to your back end.

    I prefer the latter approach. It's a little more work the first time through, but it's also more general purpose and makes for generally better code in the long run.

    Although technically you could use JavaScript, I wouldn't suggest it; why go backwards in tech if you don't have to?

    0 讨论(0)
  • 2021-01-16 17:16

    By ASP.NET, do you mean an ASP.NET Webforms app?

    If so, an ASP.NET Webforms app is a method of building a UI. What you need is an API, for your Silverlight app to use programatically. For this purpose you may want to consider building an ASP.NET Webservice instead, which provides an API over HTTP.

    0 讨论(0)
  • 2021-01-16 17:19

    here i used Uri Class to send parameter to asp.net, but you can send string format only.

    // this code written on Silverlight Button Click Event.

    Uri myURI = new Uri(HtmlPage.Document.DocumentUri,String.Format("Report.aspx?brcd={0}&acc={1}&user={2}", Brcd, Acc, User)); HtmlPage.Window.Navigate(myURI, "_blank");
    

    below code is written on Asp.net page_load or page init event

     Brcd = Request.QueryString["brcd"];// brcd value accept here.
     acc= Request.QueryString["ACC"];`
     user= Request.QueryString["User"];
    

    in above code we accept the silverlight parameter in asp.net but in [] bracket put name as it is use in silverlight page because it case sensitive.

    0 讨论(0)
  • What do you need its to send data to web server from a Silverlight application, right?

    You can:

    • Call Javascript functions from Silverlight and, there, do a postback
    • Call web services with Silverlight, but make sure its in same server which your SL application came from, or you will face some XSS issues.
    0 讨论(0)
提交回复
热议问题