简体   繁体   English

将参数从Silverlight传递到ASP.net

[英]Passing parameters from silverlight to ASP.net

I've made a little game in silverlight that records users scores whilst they play. 我已经在Silverlight中制作了一个小游戏,可以记录用户玩游戏时的得分。

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. 我决定,如果可以实施排行榜,那就更好了,因此我在mySQL中创建了一个数据库,用于存储所有高分以及姓名和日期。 I have created some communications to the database in ASP.net. 我已经创建了一些与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. 现在是时候将silverlight项目与ASP.net数据库通信链接起来了,因此我可以将用户名和得分作为变量发送到我的ASP.net代码中,然后将其上传到数据库中。 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, Lloyd 在此先感谢,劳埃德

At first you need add Generic Handler to your ASP.Net project. 首先,您需要将通用处理程序添加到ASP.Net项目中。

  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: 在您需要从SilverLight应用程序调用此处理程序后:

         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));

here i used Uri Class to send parameter to asp.net, but you can send string format only. 在这里,我使用Uri类将参数发送到asp.net,但是您只能发送字符串格式。

// this code written on Silverlight Button Click Event. //此代码写在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 下面的代码写在Asp.net page_load或页面初始化事件上

 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. 在上面的代码中,我们在asp.net中接受silverlight参数,但在[]括号中放置了名称,因为它区分大小写,因此在silverlight页面中使用了该名称。

By ASP.NET, do you mean an ASP.NET Webforms app? 所谓ASP.NET,是指ASP.NET Webforms应用程序吗?

If so, an ASP.NET Webforms app is a method of building a UI. 如果是这样,则ASP.NET Webforms应用程序是一种构建UI的方法。 What you need is an API, for your Silverlight app to use programatically. 您需要一个API,Silverlight应用程序才能以编程方式使用。 For this purpose you may want to consider building an ASP.NET Webservice instead, which provides an API over HTTP. 为此,您可能需要考虑构建ASP.NET Web服务,该服务通过HTTP提供API。

What do you need its to send data to web server from a Silverlight application, right? 从Silverlight应用程序向Web服务器发送数据需要什么,对吗?

You can: 您可以:

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. 一种简单的方法是让Silverlight代码通过将信息编码到查询字符串中并在服务器上调用.aspx页来创建REST URL。 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. 或者,您可以从Silverlight到后端进行Web服务调用。

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; 尽管从技术上讲您可以使用JavaScript,但我不建议您使用。 why go backwards in tech if you don't have to? 如果不需要,为什么还要落后于技术?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM