MVC WebApi的两种访问方法
//UserInfoController
using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace WebApiExam.Controllers
{
public class UserInfoController : ApiController
{
// GET: api/UserInfo
public IEnumerable<UserInfo> Get()
{
List<UserInfo> list = new List<UserInfo>();
list.Add(new UserInfo() { Id = 1, Name = "zhangsan" });
list.Add(new UserInfo() { Id = 2, Name = "lisi" });
list.Add(new UserInfo() { Id = 3, Name = "wangwu" });
return list;
//return new string[] { "value1", "value2" };
}
// GET: api/UserInfo/5
public string Get(int id)
{
return "value";
}
// POST: api/UserInfo
public void Post([FromBody]string value)
{
}
// PUT: api/UserInfo/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE: api/UserInfo/5
public void Delete(int id)
{
}
}
}
//客户端访问,只能在本域中
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script>
$(function () {
loadList();
});
function loadList() {
$.ajax({
type: 'get',
data: '{}',
url:'http://localhost:10536/api/UserInfo',
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (list) {
listBody = $('#listBody');
listBody.empty();
$.each(list, function (index,item) {
listBody.append('<tr><td>' + item.Id + '</td><td>' + item.Name + '</td></tr>');
})
}
});
}
</script>
</head>
<body>
<table border="1">
<tr>
<th>编号</th>
<th>姓名</th>
</tr>
<tbody id="listBody">
</tbody>
</table>
</body>
</html>
//使用HttpClient访问,可以跨域
using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
namespace WebApiClient.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("http://localhost:10536/api/UserInfo").Result;
//方法ReadAsAsync(),必须引用项目Packages里面的System.Net.Http.Formatting.dll才可以使用
var list = response.Content.ReadAsAsync<List<UserInfo>>().Result;
ViewData.Model = list;
return View();
}
}
}
//Index.cshtml
@model List<ClassLibrary.UserInfo>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table border="1">
<tr>
<th>编号</th>
<th>名字</th>
</tr>
@foreach(ClassLibrary.UserInfo user in Model)
{
<tr>
<td>@user.Id</td>
<td>@user.Name</td>
</tr>
}
</table>
</div>
</body>
</html>
MVC WebApi的两种访问方法的更多相关文章
- js对象的 两种访问方式
来对象访问属性有两种方式.有一个对象Obj = {"Name":"Langshen","AGE":"28"} 用点访问, ...
- bind()函数的深入理解及两种兼容方法分析
在JavaScript中,bind()函数仅在IE9+.Firefox4+.Chrome.Safari5.1+可得到原生支持.本文将深入探讨bind()函数并对两种兼容方法进行分析比较.由于本文将反复 ...
- angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用
今天我们要讲的是ng2的路由系统. 例子
- git两种合并方法 比较merge和rebase
18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用m ...
- 两种Ajax方法
两种Ajax方法 Ajax是一种用于快速创建动态网页的技术,他通过在后台与服务器进行少量的数据交换,可以实现网页的异步更新,不需要像传统网页那样重新加载页面也可以做到对网页的某部分作出更新,现在这项技 ...
- mysql in 的两种使用方法
简述MySQL 的in 的两种使用方法: 他们各自是在 in keyword后跟一张表(记录集).以及在in后面加上字符串集. 先讲后面跟着一张表的. 首先阐述三张表的结构: s(sno,sname. ...
- C#中的两种debug方法
这篇文章主要介绍了C#中的两种debug方法介绍,本文讲解了代码用 #if DEBUG 包裹.利用宏定义两种方法,需要的朋友可以参考下 第一种:需要把调试方法改成debug代码用 #if DEBU ...
- Service的两种启动方法
刚才看到一个ppt,介绍service的两种启动方法以及两者之间的区别. startService 和 bindService startService被形容为我行我素,而bindService被形容 ...
- jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...
随机推荐
- 详解springmvc控制登录用户session失效后跳转登录页面
springmvc控制登录用户session失效后跳转登录页面,废话不多少了,具体如下: 第一步,配置 web.xml <session-config> <session-timeo ...
- 在线生成 QR Code
http://tool.oschina.net/qr 在线生成二维码(QR码)-采用ZXing与d-project
- [CSS] Build Responsive CSS Layouts with Tachyons
Building responsive css layouts is critical in any modern website. Tachyons makes this easy by desig ...
- [Vue] Create Filters in Vue.js
Just like in the command line, you can pipe a property through a filter to get a desired result. You ...
- 【hdu 1517】A Multiplication Game
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
- WIN32汇编语言中位图的使用
说到位图.我们事实上非常早就接触过.从最早接触计算机,我们应该就知道有图片这个东西,然后再进一步说,图片在电脑上有好几种格式比方jpg. gif .png.pcx.bmp等等,当中bmp格式的图片文件 ...
- Eclipse Che安装入门和使用(一)
Eclipse Che序列博文如下: 安装和调试篇:Eclipse Che安装入门和使用(一) Web进阶篇:Eclipse Che开发Spring Web应用(入门) (二) 本文摘要: Eclip ...
- 检索05 --static静态方法 和 非静态方法
C#静态变量使用static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明的变量称做非静态变量,在对象被实例化时创建,通过对象进行访问一个类的所有实例的同一C#静 ...
- JAVA SE回顾及思考(2)——数组的复制与动态扩展
我们知道在Java中数组是非基本类型既数组是对象(Object)的子类,所以用下面的这种方式是不能复制该对象的 public static void main(String[] args) { int ...
- Arcgis api for javascript学习笔记 - 不改变默认端口(6080)情况下,外网访问Arcgis Server 发布的接口
Arcgis Server发布的地图服务地址默认端口号是6080,假设本机上只对80端口做了外网映射,在IIS中部署了一个网站绑定了80端口,那么网站中某个页面通过arcgis api for js ...