在大学学期期间学习的从mvc中的webapi中取数据

直接看代码

首先是控制器中的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Security; namespace WEBAPI.Controllers
{
//简历一个类存储数据
public class Users
{
public int Userid { get; set; }
public string Username { get; set; }
public string pwd { get; set; }
}
//存储数据
public class UserController : ApiController
{
private List<Users> _userlist = new List<Users>()
{
new Users{Userid=,Username="jake1",pwd=""},
new Users{Userid=,Username="jake2",pwd=""},
new Users{Userid=,Username="jake3",pwd=""},
}; //get获取数据 //获取所有数据
//api/User/Get
public IEnumerable<Users> Get()
{ return _userlist;
}
//根据id获取当数据
public Users Get(int id)
{
dynamic u;
u = (from c in _userlist where c.Userid.Equals(id) select c).FirstOrDefault();//取到第一条数据
u = _userlist.FirstOrDefault(p => p.Userid.Equals(id));
return u; }
//根据对象获取数据
public Users GetModel([FromUri]Users u)
{
dynamic us;
/* u = (from c in _userlist where c.Userid.Equals(id) select c).FirstOrDefault();*///取到第一条数据
us = _userlist.FirstOrDefault(p => p.Userid.Equals(u.Userid)&&p.Username.Equals(u.Username));
return us; } //post获取数据 //获取所有数据
[HttpPost]
public IEnumerable<Users> GetUser()
{ return _userlist;
}
//根据id获取单个数据
[HttpPost]
public IEnumerable<Users> GetUser1([FromBody]int id)
{ return _userlist;
} }
}

视图中的

@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-3.3.1.js"></script>
</head>
<body>
get部分
<div>
<input type="button" id="btn1" value="返回所有数据" />
<br />
<input type="button" id="btn2" value="返回指定数据" />
<br />
<input type="button" id="btn3" value="封装用户数据传递" />
<br /> </div> post部分
<div>
<input type="button" id="btn4" value="返回所有数据" />
<br />
<input type="button" id="btn5" value="返回指定数据" />
<br />
<input type="button" id="btn6" value="封装用户数据传递" />
<br />
用户名:<input type="text" id="txtname" name="txtname" />
密码:<input type="text" id="txtpwd" name="txtpwd" />
<input type="button" value="登录" />
</div>
<script>
$(function () {
$("#btn1").click(function () {
$.ajax({
type: 'GET',
url: '/api/User/Get',
dataType: 'json',
success: function (res) {
alert(res);
}
})
}) $("#btn2").click(function () {
$.ajax({
type: 'GET',
url: '/api/User/Get/1',
dataType: 'json',
success: function (res) {
alert(res);
}
})
}) $("#btn3").click(function () {
var user = { Userid: , Username:'jake', pwd: '' };
$.ajax({
type: 'GET',
url: '/api/User/GetModel/',
dataType: 'json',
data: user,
success: function (res) {
alert(res);
}
})
}) $("#btn4").click(function () { $.ajax({
type: 'POST',
url: '/api/User/GetUser/',
dataType: 'json', success: function (res) {
alert(res);
}
})
}) $("#btn5").click(function () {
var d = { "": "" };
$.ajax({
type: 'POST',
url: '/api/User/GetUser1/',
dataType: 'json',
data:d,
success: function (res) {
alert(res);
}
})
}) })
})
</script>
</body>
</html>

WEBAPI获取数据的更多相关文章

  1. webapi 获取json数据

    一般的我们可以直接使用参数来接受,这个就不多介绍了 [HttpGet] public IHttpActionResult Test2([FromUri]string name) { object ob ...

  2. C#(在WeBAPI)获取Oracle(在PL/SQL)游标类型的存储过程(用到了RefCursor)

    需求:WebAPI服务端,通过Oracle数据库的存储过程,获取数据. 在PL/SQL 建立存储过程:(先来最简单的,就是把整个表都查出来) create or replace procedure S ...

  3. Extjs4.2+webAPI+EF实现分页以及webapi的数据传值

    由于不明白分页的总数是怎么计算,不知道他的分页方式所以花费了好多功夫,现在弄出来了与大家分享下 1.首先是EF的简历,想必大家都清楚:添加-〉新建项-〉数据-〉Ado.net实体数据模型 2.就是后台 ...

  4. Webservice WCF WebApi 前端数据可视化 前端数据可视化 C# asp.net PhoneGap html5 C# Where 网站分布式开发简介 EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下? SQL Server之深入理解STUFF 你必须知道的EntityFramework 6.x和EntityFramework Cor

    Webservice WCF WebApi   注明:改编加组合 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下, ...

  5. WCF+Restfull服务 提交或获取数据时数据大小限制问题解决方案

    近日在使用wcf的restfull架构服务时遭遇到了提交大数据的问题. 大数据包含两种情形: 1)单条数据量过大. 2)提交或获取的数据条数过多. 在测试时发现,默认设置下当单条JSON数据大于30K ...

  6. python httplib get和post获取数据

    httplib 下的 status http请求的状态  200 404 500... reason 返回答复 OK或者 FAULRE read()  读取内容 get方法: #!/usr/bin/e ...

  7. 从SQLite获取数据完成一个产品信息展示

    在ios实际开发当中,我们常常用到Core Data做为数据储存首选.但在处理一些大量复杂的数据值且数据之间相互关联的时候,这就不得不使用关系型数据库来实现.例如一个导航程序,自身应该包含大量的地图自 ...

  8. MySQL随机获取数据的方法,支持大数据量

    最近做项目,需要做一个从mysql数据库中随机取几条数据出来. 总所周知,order by rand 会死人的..因为本人对大数据量方面的只是了解的很少,无解,去找百度老师..搜索结果千篇一律.特发到 ...

  9. AngularJS SQL 获取数据

    使用PHP从MySQL中获取数据: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

随机推荐

  1. Add a Class from the Business Class Library 从业务类库添加类 (XPO)

    In this lesson, you will learn how to use business classes from the Business Class Library as is. Fo ...

  2. bug调试宝典

    bug调试技巧 宝典之一 : 坚信一个原则,程序不会说谎,一定是有原因的 多数的错误表现看起来莫明奇妙,甚至不可思议 但当我们找到问题后会发现:其实问题的根源是如此简单.如此的合乎道理. 这就要求我们 ...

  3. 滑动门出现的背景---实例微信导航栏(a盒子里面包span盒子,文字写在span里)

    需求: 制作网页时,为了美观,常常需要为网页元素设置特殊形状的背景,比如微信导航栏,有凸起和凹下去的感觉,其中最大的问题是字数不同,如何做? 解决: 用一个a包含span来制作,字数放在span里面. ...

  4. MYSQL 复制整个数据库

    同一个服务器 mysqldump old -u root -p123456  | mysql new -u root -p123456 不同服务器(在源数据库服务器上执行) mysqldump old ...

  5. [转]C#操作Outlook

    本文转自:https://blog.csdn.net/yanlovehan/article/details/8500449 //引用Microsoft.Office.Interop.Outlook.d ...

  6. scala 语法速查

    摘自官网 variables   var x = 5 Good x = 6 Variable. val x = 5 Bad x = 6 Constant. var x: Double = 5 Expl ...

  7. Mysql 连接提示 Client does not support authentication protocol requested by server 客户端不支持服务器请求的身份验证协议;考虑升级MySQL客户端

    由于查阅了很多百度文档发现很多方法比较复杂,所以写个备忘: 首先,进入MySQL 8.0Command Line Client -Unicode,输入密码,登录进去. 然后,在命令行输入:ALTER ...

  8. 常见的Dos命令大全

    打开cmd: Win键+R  输入cmd; 常用的Dos命令: 1.盘符切换: 2.打开文件目录:   dir 3.清理屏幕:  cls 4.退出: exit 5.查看本机IP地址:ipconfig ...

  9. pycharm安装pymysql包

    1.为什么? 每个程序连接数据库,python开发2.*版本可以直接使用MySQL,python3.*版本需要下载使用PyMySQL包才能连接数据库... 2. 怎么做? 2.1. 更改源:pycha ...

  10. web 服务

    package main import ( "strings" "fmt" "net/http" "log" ) fun ...