ajax请求类库

  <system.webServer>
    <handlers>
      <!--type为处理jsonHandler的命名空间加类名-->
      <!--第二个为程序集的名称-->
      <add name="JsonApi" verb="*" path="/Json/*" type="JsonFactory.JsonHandler,JsonFactory" />
    </handlers>

  </system.webServer>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.SessionState;


namespace JsonFactory
{
    public class JsonHandler : IHttpHandler, IRequiresSessionState
    {
        public bool IsReusable
        {
            get { return false; }
        }


        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetExpires(DateTime.Now);
            context.Response.ContentType = jsonType;
            this.SetResponse(context);
            context.Response.End();
        }


        private const string jsonType = "application/json";
        private const string javascriptType = "application/javascript";




        private void SetResponse(HttpContext context)
        {
            string[] segments = context.Request.Url.Segments;
            string action = "";
            if (segments.Length < 3)
            {
                return;
            }
            action = segments[2].Trim('/').ToLower();
            JsonBase json = null;
            switch (action)
            {
                case "user":
                    json = new InformationJsonData(context.Request);
                    break;
                default:
                    break;
            }
            if (json == null)
            {
                return;
            }


            string responseString = json.OutputData();


            //var compress = new PageCompression(context);
            //compress.GZipEncodePage();


            context.Response.Write(responseString);
        }
    }
}


ajax请求类似路由API(MVC):

 //API路由
            var formatters = GlobalConfiguration.Configuration.Formatters;
            formatters.Remove(formatters.XmlFormatter);
            System.Web.Routing.RouteTable.Routes.MapHttpRoute("DefaultAPi",
                "api/{controller}/{action}/{id}",
                new { id = RouteParameter.Optional });

  public class MyController : ApiController
    {
        /// <summary>
        /// 获取单个出团日期日期数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AcceptVerbs("POST", "GET")]
        public string GetHelloWorld(int id)
        {
            return "hello world" + id;
        }
      

       $(function () {
            /*
            $.post("/Json/Information/admin", function (d) {
                alert(d);
            });*/
            var id = 1;
            $.post("/API/My/GetHelloWorld/" + id, function (d) {
                alert(d);
            });
        })

    }


转载于:https://www.cnblogs.com/wangyhua/p/4050592.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值