zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Restful API
    Vue之指令
    Scrapy框架
    爬虫提高性能:串行、线程进程、异步非阻塞
    MongoDB
    Beautifulsoup模块
    请求库之selenium
    php 正则匹配中文
    Javascript的"预编译"思考
    PHP程序员面试技巧之口试题分享
  • 原文地址:https://www.cnblogs.com/gouyanfeng/p/3173508.html
Copyright © 2011-2022 走看看