• ASP.NET WebForm中使用WebApi


    添加webapi.dll 可现在添加。

    在WebForm使用WebApi需要在全局文件里配置路由。

            using System.Web.Routing;
    
            protected void Application_Start(object sender, EventArgs e)
            {
    
                RegisterRoutes(RouteTable.Routes);
            }
    
            public static void RegisterRoutes(RouteCollection routes)
            {
                //ContactsApi为暴露的类里面为暴露的方法  API要映射的路径
                routes.MapServiceRoute<ContactsApi>("API");
    
    
            }
    ContactsApi类的定义
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using WebAPI.Resources;
    
    namespace WebAPI.APIs
    {
        [ServiceContract]
        public class ContactsApi
        {
    
            //设置为默认方法
            [WebGet(UriTemplate = "")]
            public IQueryable<Contact> Get()
            {
                var contacts = new List<Contact>()
                                {
                                new Contact {ContactId =1, Name ="1111"},
                                new Contact {ContactId =2, Name ="333"},
                                new Contact {ContactId =3, Name ="Glenn Block"},
                                new Contact {ContactId =4, Name ="Howard Dierking"},
                                new Contact {ContactId =5, Name ="Jeff Handley"},
                                new Contact {ContactId =6, Name ="Yavor Georgiev"}
                                };
                return contacts.AsQueryable();
            }
    
        }
    }

    访问地址为:http://localhost:9000/API

  • 相关阅读:
    OneNote 2010 文字识别
    Windows 7 添加网络共享打印机
    logstash
    filebeat
    记elk打包时的问题
    elasticsearch.yml
    zabbix 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作
    mac装brew
    snmp监控
    博科光纤交换机端口别名映射脚本
  • 原文地址:https://www.cnblogs.com/gouyanfeng/p/3173508.html
Copyright © 2020-2023  润新知