My Ajax Server framework-(1) JS directly calls the C # Method

Source: Internet
Author: User

Note: the framework described in this article has a new version,Click the link below to read.
[Write your own ASP. net mvc Framework]

Return to the directory: bask in my Ajax Server framework

In Ajax websites, Javascript often requires requests from the server. For example, you can submit a small State modification request, obtain the JOSN string of an object, and obtain a small HTML segment.

This function provided by the Ajax Server framework of FishWebLib allows you to directly call a C # method in Javascript to meet the above requirements. The sample code is as follows:

C # Method

Namespace MyLab. ajaxService {// <summary> // Ajax service class, which provides "Product record" related operations /// </summary> public class AjaxProduct {public int ChangeProductQuantity (int productId, int quantity) {if (productId <0) throw new MyMessageException ("ProductID not specified"); return BllFactory. getProductBLL (). changeProductQuantity (productId, quantity );}}}

Note: This framework does not have any special restrictions on the number of parameters of the method and the type of returned values. You can specify them as required.
In the sample code, although the return value is defined as the int type, you can use other types, or even no return value.

Javascript call code

function SetQuantityTextboxEvent(){    $("input.quantityTextbox").change(function(){        var j_textbox = $(this);        var recId = j_textbox.attr("pid");        var newValue = j_textbox.val();        $.ajax({            dataType: "text", type: "POST",            url: "/AjaxProduct.ChangeProductQuantity.cs",            data: {productId: recId, quantity: newValue  },            success: function (responseText) {                // ............            }        });    });}

C # method parameter format

The sample code above contains two parameters: productId and quantity.
In fact, it can also be a custom type, but you need to define productId and quantity as attributes or fields. Set the accessibility to public.
Do not modify the JS Code at all.

C # The method can be an instance method or a static method. The class can be static or non-static.

Because the ". cs" file cannot be accessed directly, the following configuration is required:

Or do not use the above configuration, but need an ashx Processor

Public class ajax: IHttpHandler {// Note: // In the example of this website, some URLs called by JS such as: URL: "/AjaxOrder. addOrder. cs "// due ". cs "is generally forbidden by Asp.net. // If you have no chance to modify IIS-level settings or Web. the preceding format cannot be used, but only the URL in this format can be used: url: "handler/ajax. ashx? Class = AjaxOrder & method = AddOrder "// The meaning of the class in the URL parameter is: Specify the Ajax service class to be called (including the namespace). The meaning of the method is: specifies the method to call. //// This is also the significance of the current file "ajax. ashx. // In this file, you only need to simply "Forward" the call. //// If you think the names of the class and method parameters are inappropriate, you can also use this method to "redefine". // you can call FishWebLib at last. ajax. methodExecutor. processRequest (HttpContext context, Type type, string method) // or: ProcessRequest (HttpContext context, string AssemblyName, string className, string method) // assemblyName specifies all types of program assembly that can be called by Ajax. Private static readonly string assemblyName = typeof (MyLab. AjaxService. AjaxCustomer). Assembly. ToString (); public void ProcessRequest (HttpContext context) {// call forwarding. The second parameter indicates the program set in which the class to be called is located. // This overload requires that these two parameter items exist in the URL parameter: class, method FishWebLib. Ajax. MethodExecutor. ProcessRequest (context, assemblyName );}}

Javascript call code

$.ajax({    url: "ajax.ashx?class=AjaxProduct&method=ChangeProductQuantity",     data: {productId: recId, quantity: newValue  },    // ......});

Now, this demo is written here. For more details, see the user manual.

Return to the directory: bask in my Ajax Server framework

Click here to go to the demo and download page

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.