简单记录在Visual Studio 2013中创建ASP.NET Web API 2
在很多跨平台的应用中就需要Web API ,比如android与数据库的交互。
Create a Web API Project
选择新建项目下的模板下的Visual C#节点下的Web节点,在模板列表下选择ASP.NET Web 应用程序,并命名为ChatApp就可以了。

在新建ASP.NET项目下选择Web API 点击确定就可以了。

Adding a Model
在解决方案下新建一个Model
//新建Model User
namespace APP_Chat.Models
{
public class User
{
public string UID { get; set; }
public string LoginName { get; set; }
public string Name { get; set; }
public string Pwd { get; set; }
public DateTime CreateTime { get; set; } }
}
//新建Model ResponseLoginState
namespace APP_Chat.Models
{
public class ResponseLoginState
{
public User user { get; set; }
public int state { get; set; }
public string msg { get; set; }
}
}
// 新建Model RequestLogin
namespace APP_Chat.Models
{
public class RequestLogin
{
public string LoginName { get; set; }
public string Pwd { get; set; }
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Adding a Controller
在解决方案下新建一个控制器

选择一个空的模板即可

下面就添加控制器的名称(Controller命名是有规范的,不能更改后面的Controller部分也不能在后门添加字符,不然都会导致最后无法访问到这个Controller像这个Controller就是通过/api/ChatApp/访问的,当然默认是Get的请求方式)

namespace APP_Chat.Controllers
{
public class ChatAppController : ApiController
{ /// <summary>
/// 用户登录
/// </summary>
/// <param name="user">用户的登录名 - LoginName,密码 - Pwd</param>
/// <returns>登录成功则返回用户的信息,和state=1</returns> [HttpPost] http 请求方式
[HttpPost]
public ResponseLoginState Login(RequestLogin user)
{
if (string.IsNullOrWhiteSpace(user.LoginName))
return new ResponseLoginState() { state = 0, msg = "参数错误,LoginName未传出!" };
if (string.IsNullOrWhiteSpace(user.Pwd))
return new ResponseLoginState() { state = 0, msg = "参数错误,Name未传出!" }; #region
using (var conn = new System.Data.OracleClient.OracleConnection(OracleHelper.ConnString))
{
conn.Open();
var command = conn.CreateCommand();
command.Parameters.Clear();
command.Parameters.Add(new OracleParameter(":loginName", user.LoginName));
command.Parameters.Add(new OracleParameter(":pwd", user.Pwd));
command.CommandText = "select * from APP_ChatUser where loginname=:loginName and pwd=:pwd";
var reader = command.ExecuteReader();
User loginuser = new User();
try
{
reader.Read();
loginuser.UID = reader["USERID"].ToString();
loginuser.LoginName = reader["LOGINNAME"].ToString();
loginuser.Name = reader["NAME"].ToString();
loginuser.Pwd = reader["PWD"].ToString();
loginuser.CreateTime = Convert.ToDateTime(reader["CreateTime"]);
return new ResponseLoginState() { state = 1, msg = "success", user = loginuser };
}
catch (Exception ex)
{ return new ResponseLoginState() { state = 0, msg = ex.Message, user = null };
}
}
#endregion
}
}
}
通过访问api/ChatApp/Login就可以访问WebApi了
JavaScript来检测WebApi
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script>
var user; function login() {
$.ajax({
url: '/api/ChatApp/Login',
type: 'POST',
dataType:'JSON',
data: { LoginName: 'zhangsan', Pwd: '123' },
success: function (data) {
if (typeof (data) != 'object')
data = JSON.parse(data);
user = data.Data.User;
alert(JSON.stringify(data));
}
}); } </script>
</head>
<body>
<input type="button" value="登录" onclick="login()" />
</body>
</html>
如果有不清楚,可以参照下面的这个网址学习http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
简单记录在Visual Studio 2013中创建ASP.NET Web API 2的更多相关文章
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(0):专题导航 [持续更新中]
写在前面的话 随着 Visual Studio 2013 的正式推出,ASP.NET 和 Visual Studio Web 开发工具 也发布了各自的最新版本. 新版本在构建 One ASP.NET ...
- 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目
注:本文是“在 Visual Studio 2013 中创建 ASP.NET Web 项目”专题的一部分,详情参见 专题导航 . 预备知识 本专题适用于 Visual Studio 2013 及以上版 ...
- 【转载】在 Visual Studio 2012 中创建 ASP.Net Web Service
在 Visual Studio 2012 中创建 ASP.Net Web Service,步骤非常简单.如下: 第一步:创建一个“ASP.Net Empty Web Application”项目 创建 ...
- Visual Studio 2010中创建ASP.Net Web Service
转自:http://blog.csdn.net/xinyaping/article/details/7331375 很多人在论坛里说,在Visual Studio 2010中不能创建“ASP.Net ...
- 在 Visual Studio 2010 中创建 ASP.Net Web Service
第一步:创建一个“ASP.Net Empty Web Application”项目 第二步:在项目中添加“Web Service”新项目 第一步之后,Visual Studio 2010会创建一个仅含 ...
- (转)在 Visual Studio 2010 中创建 ASP.Net Web Service
很多人在论坛里说,在Visual Studio 2010中不能创建“ASP.Net Web Service”这种project了,下面跟帖者云云,有的说这是因为微软已经将Web Service整合进W ...
- Visual Studio 2013 无法创建MVC项目,系统找不到指定的文件.(Exception from HRESULT:08x0070002)
在Visual Studio 2013中创建新MVC项目,(PS:现在创建个MVC项目,差点都找不到在哪,汗!-) 确定后提示,系统找不到指定的文件.(Exception from HRESULT:0 ...
- 在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移
在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移 最近发布的ASP.NET MVC 5 及Visual ...
- 在Visual Studio 2013 中使用C++单元测试
本文主要介绍在Visual Studio 2013中对代码进行单元测试的方法,包含了两方面的内容:对已有的Dll文件进行单元测试,以及对已有的源文件进行单元测试. 1. VS2013对DLL文件的单元 ...
随机推荐
- Tesseract-OCR 3.05 躲过语言文字识别(运行程序+中英日韩语言包)
最新版本 静态编译 tesseract 3.05.00dev leptonica-1.73 libgif 5.1.3 : libjpeg 8c : libpng 1.6.16 : libtiff 3. ...
- 对map创建出来的值顺序排序问题
TreeMap 在用Ajax动态创建checkBox时,而把值放在了HashMap中,hashMap是无序的,因此你动态创建出来的顺序是乱的,比如你想要的顺序是1.2.3.4但出来的结果可能是 3.2 ...
- simple demo how to get the list of online users
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq ...
- 在SpringMVC中获取request对象
1.注解法 @Autowired private HttpServletRequest request; 2. 在web.xml中配置一个监听 <listener> <listen ...
- 解决springmvc 前台取不到值
查看web.xml 如果为web-app_2_3.xsd spring不支持 修改项目为web-app_2_4.xsd
- oracle 监控
sqlplus "/as sysdba" .监控当前数据库谁在运行什么SQL语句 SELECT osuser, username, sql_text from v$session ...
- Linux Shell多命令执行
有三种: :只是顺序执行,命令之间没有任何关联,不相互影响.如 ls;date;cd /etc/ 如,创建100M的文件. && 命令之间有关系,只有前一条命令正确执行才会执行下面一 ...
- instanceof、 isinstance 与 isAssignableFrom的区别
instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化.如果被测对象是null值,则测试结果总是false. 形象地:自身实例或子类实例 instanc ...
- UVA 10192 Vacation
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...
- hdu 1005
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 思路:找规律题 #include<stdio.h> main() { ]; int ...