How to call the C # method in front-end Javascript (2) Transfer Parameters (with source code + do not input)

Source: Internet
Author: User

In the previous article http://www.bkjia.com/kf/201203/122288.html we simply implemented the specified Url can call a C # method function, but some friends mentioned, if the parameter of the method to call it? This is exactly what we will talk about in this article.
A friend in the comments replied that the content mentioned in the article is too simple and basic. In fact, this is just a matter of fact. Sharing is just to help people who need it. Some friends also said that the title is somewhat different, but there are still some associations, so let me write down this title, if there is anything wrong with it, I hope the experts will forgive me.
In the previous article, we have obtained the request method through reflection. In fact, it is very easy to get the parameters required for this method. We can use the MethodBase. GetParameters method to obtain the parameters required by this method.
Now that we know the required parameters, we can simply modify the Execute method in the Factory of the proxy class to pass the parameters.
The modified Execute method is as follows:
Void Execute (HttpContext context ){
// Obtain the class for processing the request through reflection based on the request 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 ){
// Obtain the non-parameter constructor of the class
Var constructor = type. GetConstructor (new Type [0]);
// Call the constructor to obtain an instance of the class
Var obj = constructor. Invoke (null );
// Query the Request Method
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 );
}
}
// Execute the method and output the response result
Context. Response. Write (method. Invoke (obj, args ));
}
}
}

 
Add a Plus method to the News class for testing:
 
Public int Plus (int x, int y ){
Return x + y;
}

 
Let's take a look at the Javascript code in html:
 
$ (Function (){
// Bind button click event
$ ("# BtnAction"). click (function (){
$. Post ("/Ajax/News/Plus. aspx", $ ("form"). serialize (), function (txt ){
$ ("# Result"). val (txt );
}, "Text ");
});
// $ ("Form"). serialize () generates data similar to {x: 1, y: 2}
});

 
FORM on the Html page:
 
<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>

 
Why should we use form? In fact, it's just a lazy job. I'm too lazy to knock a few more letters and use serialize in jquery to get the form data and submit it. The actual submitted data is similar to {x: 1, y: 2}. You can also manually obtain the submitted data, which may be more intuitive, but you need to click the Code:
$ (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 ");
});
});

 
After you click the equal button, the page script will send the values of the two input boxes to the server. After the server processes the values, it will respond to the output. Of course, the example task may be too simple, but it should be useful for friends who need it.
 
The purpose of publishing an article is to share a little experience, and to exchange and learn from each other. If there is something wrong with the expression in the article, I hope you will forgive me.
 
 
Attachment source code download: Click to download http://www.bkjia.com/uploadfile/2012/0308/20120308083716582.zip

 
Some other content will be added to the subsequent articles. If you are interested, please pay attention to it.
 
To be continued...

 

From Wolf Robot

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.