700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > ASP.NET WebForm中使用WebApi

ASP.NET WebForm中使用WebApi

时间:2021-10-05 04:13:44

相关推荐

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。