如何将Stringified JSON传递给C#方法?

时间:2022-10-07 15:48:53

I've spent the last night trying to figure this out.

我昨晚花了很多时间试图解决这个问题。

Basically in Google Maps, I am able to generate directions, or waypoints, between two points that the user chooses in client side Javascript.

基本上在谷歌地图中,​​我能够在用户在客户端Javascript中选择的两个点之间生成方向或路标。

I ideally want to be able to store these in my database ( Im using C#.NET and SQL Server DB ) by passing these to a server side C# method..

理想情况下,我希望能够将这些存储在我的数据库中(我使用C#.NET和SQL Server DB),将它们传递给服务器端C#方法。

I've got to the point where I can put the directions I need into a string by using:

我已经到了可以通过使用以下方式将我需要的指示放入字符串的地方:

*var string = JSON.stringify(response);*

Now, here's where I'm stuck.

现在,这就是我被困住的地方。

How do I pass this to a C# webforms method?

如何将其传递给C#webforms方法?

I've seen an MVC C# solution to my problem as:

我已经看到了我的问题的MVC C#解决方案:

var str = JSON.stringify(data)

var city = {};
        city.Directions = str;
        $.ajax({
            type: 'POST',
            url: 'usertrip.aspx/GetDirections',
            data: str ,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (r) {
                alert(r.d.Directions);;
                }
        });

But I've tested, and have concluded that this won't work for webforms. Does anyone know how I can alter this code so that I can pass the string to a Webforms method and not MVC?

但我已经测试过,并得出结论认为这对webforms不起作用。有谁知道我如何改变这段代码,以便我可以将字符串传递给Webforms方法而不是MVC?

Thanks!

2 个解决方案

#1


4  

You can definitely do this kind of thing with webforms. The important thing is that you need to set up a webservice that exposes methods that can be hit by the ajax call. This awesome article called Using jQuery to directly call ASP.NET AJAX page methods proved invaluable for me to figure out how to accomplish what you're trying to do.

你绝对可以用webforms做这种事情。重要的是,您需要设置一个Web服务,公开可以被ajax调用命中的方法。这篇名为使用jQuery直接调用ASP.NET AJAX页面方法的文章对我来说非常有用,可以弄清楚如何完成你想要做的事情。

For an example (from the article) doing something like this:

举个例子(来自文章)做这样的事情:

public partial class _Default : Page 
{
  [WebMethod]
  public static string DoSomething(string myJsonData)
  {
    // deserialize your JSON
    // do something cool with it
  }
}

Would allow you to hit the webmethod with your AJAX call. I can assure you that I've done this in many different asp.net solutions what don't use MVC so with a little bit of tinkering you should be able to get the information you need to your code behind.

允许你用你的AJAX调用点击web方法。我可以向你保证,我已经在许多不同的asp.net解决方案中完成了这项工作,不使用MVC,所以稍微修补一下你应该能够获得你需要的信息。

#2


2  

You would need to do something like :

您需要执行以下操作:

var str = JSON.stringify(data)

var city = {};
        city.Directions = str;
        $.ajax({
            type: 'POST',
            url: 'usertrip.aspx/GetDirections',
            data: { city: str },
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (r) {
                alert(r.d.Directions);;
                }
        });

And in the Webforms code behind:

并在Webforms代码背后:

 City city = new JavaScriptSerializer().Deserialize<City>(Request.Form["city"]);

#1


4  

You can definitely do this kind of thing with webforms. The important thing is that you need to set up a webservice that exposes methods that can be hit by the ajax call. This awesome article called Using jQuery to directly call ASP.NET AJAX page methods proved invaluable for me to figure out how to accomplish what you're trying to do.

你绝对可以用webforms做这种事情。重要的是,您需要设置一个Web服务,公开可以被ajax调用命中的方法。这篇名为使用jQuery直接调用ASP.NET AJAX页面方法的文章对我来说非常有用,可以弄清楚如何完成你想要做的事情。

For an example (from the article) doing something like this:

举个例子(来自文章)做这样的事情:

public partial class _Default : Page 
{
  [WebMethod]
  public static string DoSomething(string myJsonData)
  {
    // deserialize your JSON
    // do something cool with it
  }
}

Would allow you to hit the webmethod with your AJAX call. I can assure you that I've done this in many different asp.net solutions what don't use MVC so with a little bit of tinkering you should be able to get the information you need to your code behind.

允许你用你的AJAX调用点击web方法。我可以向你保证,我已经在许多不同的asp.net解决方案中完成了这项工作,不使用MVC,所以稍微修补一下你应该能够获得你需要的信息。

#2


2  

You would need to do something like :

您需要执行以下操作:

var str = JSON.stringify(data)

var city = {};
        city.Directions = str;
        $.ajax({
            type: 'POST',
            url: 'usertrip.aspx/GetDirections',
            data: { city: str },
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (r) {
                alert(r.d.Directions);;
                }
        });

And in the Webforms code behind:

并在Webforms代码背后:

 City city = new JavaScriptSerializer().Deserialize<City>(Request.Form["city"]);