Frontend calls background Functions

Source: Internet
Author: User
Tags tojson

Call var B = $ Ajax ("webapplication1. _ default. getdataset", ["23"]);

JS:/* full name of the method, parameter, and assembly name of the class (when the namespace of the method class is not the Assembly name, please pass the parameter )*/
Function $ Ajax (parammethod, paramvalue, paramassemblyname ){
If (! Parammethod) {dialog. Alert ("method parameters with full names missing", 1 );}
VaR result, Params = "", assemblyname, classname, methodname;
If (paramvalue) for (VAR I = 0; I <paramvalue. length; I ++) {Params + = "& Param" + I + "=" + encodeuri (paramvalue [I]) ;}; // get the Parameter
Classname = parammethod. substring (0, parammethod. lastindexof ('.'); // obtain the Class Name of the Method
Methodname = parammethod. substring (parammethod. lastindexof ('.') + 1); // obtain the method name
Assemblyname = classname. substring (0, classname. lastindexof ('.'); If (paramassemblyname) assemblyname = paramassemblyname; // obtain the namespace name
$. Ajax ({URL: "/../ajax. ashx? Assembly = "+ assemblyname +" & Class = "+ classname +" & method = "+ methodname + Params, type:" Post ", async: false,
Success: function (data) {result = eval ("(" + Data + ")");},
Error: function (data ){
Layer. MSG (data. responsetext, 2, 0 );
}
});
Return result;
}

Ajax. ashx:

Using system; using system. collections. generic; using system. web; using system. web. sessionstate; using system. data; using system. reflection; using system. io; using system. web. script. serialization;

Namespace chengrui. RSP. uilayer {// <summary> // Ajax summary /// </Summary> public class Ajax: ihttphandler, irequiressessionstate {

# Region Public void getmethod () get parameters /// <summary> /// get parameters /// </Summary> /// <Param name = "context"> interface content </param> /// <Param name = "paramfield"> parameter </param> // <returns> </returns> Public String getrequest (httpcontext context, string paramfield) {try {return context. request [paramfield] = NULL | context. request [paramfield]. tostring () = "undefined "? "": Context. request [paramfield]. tostring (). Trim () ;}catch {return string. Empty ;}# endregion

Public void processrequest (httpcontext context) {object result = NULL; try {string assemblyname = This. getrequest (context, "assembly"), classname = This. getrequest (context, "class"), methodname = This. getrequest (context, "method"); // obtain the assembly type = NULL; try {Assembly myassembly = assembly. load (assemblyname); type = myassembly. getType (classname);} catch {type = assembly. getexecutingassembly (). getType (classname );}

// If the Assembly cannot be found, all the contents in the base Directory are traversed using chengrui. rs. while obtaining method if (type = NULL) {fileinfo [] arrfileinfo = new directoryinfo (appdomain. currentdomain. relativesearchpath ). getfiles ("chengrui. rs. *. DLL "); foreach (fileinfo myfile in arrfileinfo) {type = assembly. load (myfile. name. substring (0, myfile. name. lastindexof ('. '))). getType (classname); If (type! = NULL) break ;}}

If (type! = NULL) {system. reflection. methodinfo = type. getmethod (methodname); If (methodinfo! = NULL) // determines whether the operation page has this method {object [] ARGs = new object [methodinfo. getparameters (). length]; // assign a value for the parameter of the other method name (INT I = 0; I <methodinfo. getparameters (). length; I ++) {ARGs [I] = convert. changetype (this. getrequest (context, "Param" + I. tostring (), methodinfo. getparameters () [I]. parametertype);} // execution method result = methodinfo. invoke (activator. createinstance (type), argS);} else result = "[assemblyname:" + assemblyname + "]. [classname: "+ classname + "]. [methodname: "+ methodname +"] method ";} else result =" [assemblyname: "+ assemblyname +"] Not found "]. [classname: "+ classname +"] class ";} catch (exception myex) {result = myex. message;} // context. response. write (commonmethod. tojson (result); context. response. write (tojson (result ));}

Public bool isreusable {get {return false ;}}

# Region Public static string tojson (Object OBJ) /// <summary> /// convert to JSON /// </Summary> /// <Param name = "OBJ"> </param> /// <returns> </returns> Public static string tojson (Object OBJ) {If (OBJ = NULL) OBJ = string. empty; javascriptserializer myjson = new javascriptserializer (); If (obj. getType (). name = "datatable") {datatable mydt = (datatable) OBJ; string tablename = mydt. tablename = ""? "Tablename1": mydt. tablename; dictionary <string, Object> DIC = new dictionary <string, Object> (); dic. add (tablename, rowstodictionary (mydt); Return myjson. serialize (DIC);} else if (obj. getType (). name = "dataset") {dataset myds = (Dataset) OBJ; dictionary <string, Object> DIC = new dictionary <string, Object> (); for (INT I = 0; I <myds. tables. count; I ++) {string tablename = myds. tables [I]. tablen Ame = ""? "Tablename" + I. tostring (): myds. tables [I]. tablename; dic. add (tablename, rowstodictionary (myds. tables [I]);} return myjson. serialize (DIC);} else return myjson. serialize (OBJ );}

Private Static list <dictionary <string, Object> rowstodictionary (datatable table) {list <dictionary <string, Object> objs = new list <dictionary <string, object >>> (); foreach (datarow DR in table. rows) {dictionary <string, Object> Drow = new dictionary <string, Object> (); For (INT I = 0; I <table. columns. count; I ++) {Drow. add (table. columns [I]. columnname, Dr [I]);} objs. add (Drow);} return objs ;}

# Endregion }}

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.