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