惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
T
Threatpost
N
News and Events Feed by Topic
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
D
Docker
G
Google Developers Blog
Microsoft Security Blog
Microsoft Security Blog
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
The GitHub Blog
The GitHub Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
T
Troy Hunt's Blog
Webroot Blog
Webroot Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
量子位
Apple Machine Learning Research
Apple Machine Learning Research
H
Help Net Security
F
Full Disclosure
B
Blog
O
OpenAI News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
Forbes - Security
Forbes - Security
Know Your Adversary
Know Your Adversary
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
Scott Helme
Scott Helme
T
The Exploit Database - CXSecurity.com
博客园 - 聂微东
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
Recorded Future
Recorded Future
IT之家
IT之家
Project Zero
Project Zero
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
SecWiki News
SecWiki News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - I,Robot

短网址的简单实现 怎样在前端Javascript中调用C#方法(4)验证授权 怎样在前端Javascript中调用C#方法(3)使用特性Attribute 怎样在前端Javascript中调用C#方法(1)简单实现(附源码) 闲来无事,写了段js仿google首页动画,附源码下载 终于请了个客服值夜班了,嘿嘿,不是18K月薪 我的编程之路 说再见的那一刻,我都不敢回头多看你们一眼… 终于解决了”SQL多表连接查询C#化”这的问题 趣味算法:国王和100个囚犯(据说是腾讯的面试题) 提交备案申请4年后,网站备案号终于下来了,可惜网站跟域名早已不见了 我的山寨HTC(5)-电池 我的山寨HTC(4)-U盘 我的山寨HTC(3)-WIFI 我的山寨HTC(2)-GPS 我的山寨HTC(1)-购机 eBay中国上海总部之行20091020 eBay中国2009开发者论坛上海之行 在一个没有爱心的城市,谈何文明,谈何和谐
怎样在前端Javascript中调用C#方法(2)传递参数(附源码+高手勿入)
I,Robot · 2012-03-08 · via 博客园 - I,Robot

  上一篇文章中我们简单的实现了指定Url可以调用某个C#方法的功能,但有的朋友提到了,如果带参数的方法该怎么调用呢?这正是这篇文章要说到的内容。

  评论中有朋友回复说文章中讲到的内容过于简单基础,其实这个也只是相对而言,分享出来也只是希望能够对需要的人有所帮助。也有朋友说标题有些不符,多少是有些不符,但也还是有一定的关联,所以就让我将就这个标题写下去吧,如有不妥还望众高手见谅了。
  上篇中我们已经通过反射获得了请求的方法,实际上这时候我们要拿到该方法所需要的参数已经是一件很简单的事情了。我们可以通过MethodBase.GetParameters方法获取到该方法所需要的参数。
  既然已经知道了所需的参数,那我们简单的修改一下代理类Factory中的Execute方法,即可实现参数的传递了。
  修改后的Execute方法如下:

void Execute(HttpContext context) {
//根据请求的Url,通过反射取得处理该请求的类
string url = context.Request.Url.AbsolutePath;
string[] array = url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string typename = "Biz";
for (int x = 1; x < array.Length - 1; x++) {
typename += "." + array[x];
}
Type type = this.GetType().Assembly.GetType(typename, false, true);
if (type != null) {
//取得类的无参数构造函数
var constructor = type.GetConstructor(new Type[0]);
//调用构造函数取得类的实例
var obj = constructor.Invoke(null);
//查找请求的方法
var method = type.GetMethod(System.IO.Path.GetFileNameWithoutExtension(url));
if (method != null) {
var parameters = method.GetParameters();
object[] args = null;
if (parameters.Length > 0) {
args = new object[parameters.Length];
for (int x = 0; x < parameters.Length; x++) {
args[x] = Convert.ChangeType(context.Request.Form[parameters[x].Name], parameters[x].ParameterType);
}
}
//执行方法并输出响应结果
context.Response.Write(method.Invoke(obj, args));
}
}
}

News类中加入一个Plus方法做测试:

public int Plus(int x, int y) {
return x + y;
}

再来看看html中的Javascript代码:

$(function () {
//绑定按钮点击事件
$("#btnAction").click(function () {
$.post("/Ajax/News/Plus.aspx", $("form").serialize(), function (txt) {
$("#result").val(txt);
}, "text");
});
//$("form").serialize()生成的数据类似于{x:1,y:2}
});

Html页面中的FORM:

<form  action="">
<input type="text" name="x" value="1" class="txt" />
<input type="button" disabled="disabled" value="+" class="txt btn" />
<input type="text" name="y" value="2" class="txt" />
<input id="btnAction" type="button" value="=" class="txt btn" />
<input type="text" readonly="readonly" id="result" class="txt" />
</form>

为什么要用form,其实也就是懒,懒得敲多几个字母,直接用jquery中的serialize获取表单数据并提交。实际提交的数据类似于{x:1,y:2},要想手动获取提交的数据也可以,这样可能更直观些,但就是要多敲点代码:

$(function () {
$("#btnAction").click(function () {
$.post("/Ajax/News/Plus.aspx", {x:$("input[name='x']").val(),y:$("input[name='y']").val()}, function (txt) {
$("#result").val(txt);
}, "text");
});
});

点击等于按钮后,页面脚本会将两个输入框的值发送到服务端,服务端处理后将值响应输出。当然,示例的任务可能过于简单,但对于有需要的朋友,应该能找到它的用处。

发布文章的目的,一个是分享一点点经验,另一个是希望能跟大家一起交流互相学习,如果文中有表达不到位的,或者不妥之处,还望大家见谅。

后续还会有一些其它的内容加入到后面文章中,请有兴趣的同学不妨留意一下。

未完待续...