vs2010-webapi开发http请求以及website中如何实现http请求
作者:快盘下载 人气:一、vs2010 webapi开发
第一步;创建
第二步;离线安装NuGet
1、复制链接到浏览器打开;http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c
2、点击下载
3、双击安装
4、重新打开vs可查看到NuGet
5、加载 Microsoft.AspNet.WebApi
Install-Package Microsoft.AspNet.WebApi -Version 4.0.30506
安装成功后;会引用webapi需要用到的dll
Microsoft.Web.Infrastructure
System.Net.Http
System.Web.Http
6、在项目新增App_Start文件夹;并创建WebApiConfig.cs;用于添加api的路由配置
using System.Web.Http; public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: ;DefaultApi;, routeTemplate: ;api/{controller}/{action}/{id};, defaults: new { action = RouteParameter.Optional, id = RouteParameter.Optional } ); } }
7、在Global.asax的Application_Start中注册WebApiConfig
Global.asax
<% Application Codebehind=;Global.asax.cs; Inherits=;WebApiTestPro.Global; Language=;C#; %>
Global.asax.cs
void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 WebApiConfig.Register(System.Web.Http.GlobalConfiguration.Configuration); }
8、新增apiControllers进行测试
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using WebApiTestPro.Controllers; using WebApiTestPro.Models; using System.Net.Http; using Newtonsoft.Json; using System.Text; namespace WebApiTestPro.Controllers.Api { public class TestController :ApiController { //GET api/<controller> [HttpGet] public HttpResponseMessage Get() { var product = new { id = 1, name = ;testName; }; HttpResponseMessage result = new HttpResponseMessage(); result.Content = new StringContent(JsonConvert.SerializeObject(product), Encoding.GetEncoding(;UTF-8;), ;application/json;); return result; } [HttpPost] public ResultModel PostTest(TestModel model) { ResultModel result = new ResultModel(); ResultData data = new ResultData(); result.res = true; data.res_code = ;200;; data.fail_msg = ;default;; data.timestamp = model.timestamp; data.content = model.content; result.res_data = data; return result; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WebApiTestPro.Models { public class ResultData { public string res_code { get; set; } public string fail_msg { get; set; } public string sign { get; set; } public string timestamp { get; set; } public List<String> content { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApiTestPro.Models { public class ResultModel { public Boolean res { get; set; } public ResultData res_data { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApiTestPro.Models { public class TestModel { /// <summary> /// 学生Id /// </summary> public int Id { get; set; } /// <summary> /// 学生姓名 /// </summary> public string Name { get; set; } public string timestamp { get; set; } public List<String> content { get; set; } public string sign { get; set; } } }
项目目录如下;
至此;vs2010.net webapi开发测试完成;
二、在website项目中添加http接口请求
网站系统目录Bin下;点击右键;添加引用即可;
添加引用后启动;出现一个问题;提示json包版本没有找到;解决方案;在web.config中添加以下配置即可
<runtime> <assemblyBinding xmlns=;urn:schemas-microsoft-com:asm.v1;> <dependentAssembly> <assemblyIdentity name=;Newtonsoft.Json; publicKeyToken=;30AD4FE6B2A6AEED; culture=;neutral;/> <bindingRedirect oldVersion=;0.0.0.0-6.0.0.0; newVersion=;6.0.0.0;/> </dependentAssembly> </assemblyBinding> </runtime>
加载全部内容