[Reprint] multiple Ajax calls on An ASPX or ashx page

Source: Internet
Author: User
Tags switch case

In the Asp.net program developed with Ajax. use the ashx page to interact with the front-end page. however, each Ajax interaction requires an ashx page. the result is a lot of ashx pages in the project. make the project difficult to manage. now we can try to allow multiple Ajax interactions on an ashx page;

The content of ajaxtest.htm on the front page is as follows:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> This page interacts with the background in different ways </title>
<SCRIPT type = "text/JavaScript" src = "JS/jquery-1.4.2.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">

// Use the jquery library for Ajax Interaction
$ (Document). Ready (function (){

 // Execute an Ajax request. The command tells the backend method to call.
$. Get ("handler. ashx", {command: "Method1", value: "chentao"}, function (data ){
Alert (data );
});

// Make an Ajax request, and the Command tells the background to call the method2 Method


$. Get ("handler. ashx", {command: "method2", value: "tangyu"}, function (data ){

Alert (data );
})

</SCRIPT>
</Head>
<Body>

</Body>
</Html>

Set up a handler. ashx page in the background as follows:

<% @ Webhandler Language = "C #" class = "handler" %>

Using system;
Using system. Web;

Public class handler: ihttphandler {

Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";

If (context. request ["command"]! = NULL)

{

// Obtain the Command sent from the foreground and determine the method to call.

String command = context. request ["command"]. tostring ();
String data = context. request ["value"]. tostring ();
Switch (command)
{
Case "Method1 ":
Method1 (context );
Break;
Case "method2 ":
Method2 (context );
Break;
Default:
Break;
}
}

}
 
Public bool isreusable {
Get {
Return false;
}
}
Public void Method1 (httpcontext context)
{

Context. response. Write ("hello," + context. request ["value"]. tostring ());


}
Public void method2 (httpcontext context)
{
Context. response. Write ("hello," + context. request ["value"]. tostring ());
}

 

}

If there are multiple methods, there will be many judgments in the switch case. Consider using a simpler method. Use reflection.

<% @ Webhandler Language = "C #" class = "handler" %>

Using system;
Using system. Web;

Public class handler: ihttphandler {

Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
If (context. request ["command"]! = NULL)
{

//
String command = context. request ["command"]. tostring ();
System. reflection. methodinfo method = This. GetType (). getmethod (command );
If (method! = NULL)
{
Method. Invoke (this, new object [] {context });
}
}

}
 
Public bool isreusable {
Get {
Return false;
}
}
Public void Method1 (httpcontext context)
{

Context. response. Write ("hello" + context. request ["value"]. tostring ());


}
Public void method2 (httpcontext context)
{

Context. response. Write ("hello," + context. request ["value"]. tostring ());
}

 

}

Using Reflection greatly simplifies the program.

========================================================== ==================

Use the ASPX page to interact with Ajax

Create An ASPX page webmethod. aspx

Delete the remaining parts of the webmethod. ASPX page and retain only

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "webmethod. aspx. cs" inherits = "webmethod" %>

This statement

The content of webmethod. aspx. CS is as follows:

Using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;

Using system. Web. Services;
Using system. reflection;

Public partial class webmethod: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String methodname = httpcontext. Current. Request. pathinfo. substring (1 );
// Response. Write (methodname );
Methodinfo method = This. GetType (). getmethod (methodname );
If (method! = NULL)
{
Response. Write (method. Invoke (this, new object [] {});
}
// Response. Write (getresult ());

}
[Webmethod (enablesession = true)]
Public String getresult ()
{
// Return "hello ";
If (httpcontext. Current. request ["name"]! = NULL)
{
String value = httpcontext. Current. request ["name"]. tostring ();
// Httpcontext. Current. Request. pathinfo;
Return "{'name': '" + value + "'}";
}
Else
{
Return "{Name: 'error '}";
}
}
}

Perform Ajax interaction between the test.html page and the webmethod. ASPX page. Content of the test.html page

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> use the ASPX page for interaction </title>
<SCRIPT type = "text/JavaScript" src = "JS/jquery-1.4.2.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){

$. Ajax ({
Type: "Post ",

URL: "webmethod. aspx/getresult ",
Data: "name = chentao ",
Datatype: "text ",
Success: function (d ){
Alert (d );
}

});
});
</SCRIPT>
</Head>
<Body>

</Body>
</Html>

 

In the Asp.net program developed with Ajax. use the ashx page to interact with the front-end page. however, each Ajax interaction requires an ashx page. the result is a lot of ashx pages in the project. make the project difficult to manage. now we can try to allow multiple Ajax interactions on an ashx page;

The content of ajaxtest.htm on the front page is as follows:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> This page interacts with the background in different ways </title>
<SCRIPT type = "text/JavaScript" src = "JS/jquery-1.4.2.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">

// Use the jquery library for Ajax Interaction
$ (Document). Ready (function (){

 // Execute an Ajax request. The command tells the backend method to call.
$. Get ("handler. ashx", {command: "Method1", value: "chentao"}, function (data ){
Alert (data );
});

// Make an Ajax request, and the Command tells the background to call the method2 Method


$. Get ("handler. ashx", {command: "method2", value: "tangyu"}, function (data ){

Alert (data );
})

</SCRIPT>
</Head>
<Body>

</Body>
</Html>

Set up a handler. ashx page in the background as follows:

<% @ Webhandler Language = "C #" class = "handler" %>

Using system;
Using system. Web;

Public class handler: ihttphandler {

Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";

If (context. request ["command"]! = NULL)

{

// Obtain the Command sent from the foreground and determine the method to call.

String command = context. request ["command"]. tostring ();
String data = context. request ["value"]. tostring ();
Switch (command)
{
Case "Method1 ":
Method1 (context );
Break;
Case "method2 ":
Method2 (context );
Break;
Default:
Break;
}
}

}
 
Public bool isreusable {
Get {
Return false;
}
}
Public void Method1 (httpcontext context)
{

Context. response. Write ("hello," + context. request ["value"]. tostring ());


}
Public void method2 (httpcontext context)
{
Context. response. Write ("hello," + context. request ["value"]. tostring ());
}

 

}

If there are multiple methods, there will be many judgments in the switch case. Consider using a simpler method. Use reflection.

<% @ Webhandler Language = "C #" class = "handler" %>

Using system;
Using system. Web;

Public class handler: ihttphandler {

Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
If (context. request ["command"]! = NULL)
{

//
String command = context. request ["command"]. tostring ();
System. reflection. methodinfo method = This. GetType (). getmethod (command );
If (method! = NULL)
{
Method. Invoke (this, new object [] {context });
}
}

}
 
Public bool isreusable {
Get {
Return false;
}
}
Public void Method1 (httpcontext context)
{

Context. response. Write ("hello" + context. request ["value"]. tostring ());


}
Public void method2 (httpcontext context)
{

Context. response. Write ("hello," + context. request ["value"]. tostring ());
}

 

}

Using Reflection greatly simplifies the program.

========================================================== ==================

Use the ASPX page to interact with Ajax

Create An ASPX page webmethod. aspx

Delete the remaining parts of the webmethod. ASPX page and retain only

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "webmethod. aspx. cs" inherits = "webmethod" %>

This statement

The content of webmethod. aspx. CS is as follows:

Using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;

Using system. Web. Services;
Using system. reflection;

Public partial class webmethod: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String methodname = httpcontext. Current. Request. pathinfo. substring (1 );
// Response. Write (methodname );
Methodinfo method = This. GetType (). getmethod (methodname );
If (method! = NULL)
{
Response. Write (method. Invoke (this, new object [] {});
}
// Response. Write (getresult ());

}
[Webmethod (enablesession = true)]
Public String getresult ()
{
// Return "hello ";
If (httpcontext. Current. request ["name"]! = NULL)
{
String value = httpcontext. Current. request ["name"]. tostring ();
// Httpcontext. Current. Request. pathinfo;
Return "{'name': '" + value + "'}";
}
Else
{
Return "{Name: 'error '}";
}
}
}

Perform Ajax interaction between the test.html page and the webmethod. ASPX page. Content of the test.html page

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> use the ASPX page for interaction </title>
<SCRIPT type = "text/JavaScript" src = "JS/jquery-1.4.2.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){

$. Ajax ({
Type: "Post ",

URL: "webmethod. aspx/getresult ",
Data: "name = chentao ",
Datatype: "text ",
Success: function (d ){
Alert (d );
}

});
});
</SCRIPT>
</Head>
<Body>

</Body>
</Html>

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.