简体   繁体   English

如何从Javascript调用C#函数

[英]How to call C# function from Javascript

I saw several solutions for this question. 我看到了这个问题的几种解决方案。 However when i tried myself it doesn't work well. 但是,当我尝试自己时,效果并不理想。

I tried to modify my C# code to this 我试图将我的C#代码修改为此

[WebMethod]
protected void Show(object sender, EventArgs e)
{
    //C# function
}

But There is no WebMethod thing for my asp.net. 但是我的asp.net没有WebMethod东西。 Do I need to include some libraries? 我需要包括一些库吗?

In JS am I doing right? 我在JS中做的对吗? Why the sender and e are error? 为什么发件人和e错误?

 <script> $(document).ready(function () { var image = $("#VImage").html() $("#myModal").on("shown.bs.modal", function () { PageMethods.Show(object sender, EventArgs e); }); }); </script> 

You shouldn't call a function from anywhere like that, you are calling a function in the same fashion as declaring one. 您不应该从这样的任何地方调用函数,而是以与声明函数相同的方式调用函数。

PageMethods.Show(object sender, EventArgs e);

should be: 应该:

var sender, e;
PageMethods.Show(sender, e);

The way you're calling the C# method is no different than trying to make any other AJAX call. 调用C#方法的方式与尝试进行任何其他AJAX调用没有什么不同。 You just need to make sure that the C# method is properly exposed. 您只需要确保正确公开了C#方法。 To do that you need to. 为此,您需要这样做。

  • Annotate it with [WebMethod] [WebMethod]注释
  • Also believe it needs to be public and static in this case 还认为在这种情况下,它必须是publicstatic

On the client side, since you're using JQuery call it the same way you'd make any AJAX call 在客户端,由于您使用的是JQuery调用方式,因此您可以进行任何AJAX调用

    $.ajax({
      type: "POST",
      url: "Default.aspx/Show",
      data: "{ sender: {}, e: {} }",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
          console.log(msg);
      },
      error: function (msg) {
          console.log(msg);
      }
    });

Think of it this way, you need to make an HTTP request that includes all the information that the web server needs to know to invoke the C# method. 以这种方式考虑,您需要发出一个HTTP请求,其中包含Web服务器调用C#方法所需的所有信息。 This means it needs to be know the method name and the parameters that the method needs. 这意味着需要知道方法名称和方法所需的参数。

Behind the scenes when you make the HTTP request the framework is taking the body from the HTTP POST and attempting to deserialize the JSON object to get the name for the method and parameters it needs to invoke the C# method. 当您发出HTTP请求时,该框架在幕后从HTTP POST获取主体,并尝试反序列化JSON对象以获取方法名称和调用C#方法所需的参数。

In your example the C# method has signature Show(object sender, EventArgs e) , that means the server is expecting from the client, two objects one of which it knows enough to deserialize into an EventArgs object so it can call the Show method. 在您的示例中,C#方法具有签名Show(object sender, EventArgs e) ,这意味着服务器正在等待客户端发出请求,这两个对象中的一个它知道足够反序列化为EventArgs对象,因此可以调用Show方法。

I recommend changing the parameters to primitive types ('string', 'int', etc.) if possible or creating your own parameters object that you've verified that can be serialized/deserialized by .NET. 我建议尽可能将参数更改为原始类型(“字符串”,“ int”等),或创建自己的已验证的参数对象,该对象可以由.NET进行序列化/反序列化。

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

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