1)在web.config 中system.web 节点下加入例如以下代码

<pages controlRenderingCompatibilityVersion="4.0" clientIDMode="AutoID">

      <namespaces>

        <add namespace="MyMVC"/>

      </namespaces>

    </pages>

    <httpHandlers>

      <add path="*.cspx" verb="*" type="MyMVC.AjaxHandlerFactory,MyMVC" validate="true"/>

    </httpHandlers>

2)为了兼容IIs7.0 在<system.webServer>节点下加入例如以下代码:

<validation validateIntegratedModeConfiguration="false"/>

    <security>

      <requestFiltering>

        <fileExtensions>

          <remove fileExtension=".cspx"/>

          <add fileExtension=".cspx" allowed="true"/>

        </fileExtensions>

      </requestFiltering>

    </security>

    <handlers>

      <add name="AjaxHandlerFactory" verb="*" path="*Ajax*/*.cspx"

                      type="MyMVC.AjaxHandlerFactory, MyMVC" preCondition="integratedMode"/>

    </handlers>

上边的是配置

比如在AjaxMenuManager.cs中有例如以下方法

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using MyMVC;

namespace Lesso.Invoicing.UI.AjaxMethod

{

    public class AjaxMenuManager

    {

        [Action]

        public string Test(string a)

        {

            return a;

        }

    }

}

在js中调用方法例如以下:

$.ajax({

                url: "/Lesso.Invoicing.UI.AjaxMethod/AjaxMenuManager/Test.cspx",

//Lesso.Invoicing.UI.AjaxMethod 命名空间;AjaxMenuManager 类名。Test 方法名

                data: { a: "2" },

                success: function (responseText) {

                    alert(responseText);

                },

                error: function (msg) {

                    alert(msg);

                }

            });

须要注意的是:

ajax方法的前缀为Ajax...

MyMVC 原创文章和源代码下载地址:http://www.cnblogs.com/fish-li/archive/2012/02/12/2348395.html

04-26 11:35