您好,欢迎来到纷纭教育。
搜索
您的当前位置:首页ASP.NET WebForm中使用WebApi

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();
        }

    }
}

转载于:https://www.cnblogs.com/gouyanfeng/p/3173508.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- fenyunshixun.cn 版权所有 湘ICP备2023022495号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务