在很多跨平台的应用中就需要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的更多相关文章

  1. 在 Visual Studio 2013 中创建 ASP.NET Web 项目(0):专题导航 [持续更新中]

    写在前面的话 随着 Visual Studio 2013 的正式推出,ASP.NET 和 Visual Studio Web 开发工具 也发布了各自的最新版本. 新版本在构建 One ASP.NET ...

  2. 在 Visual Studio 2013 中创建 ASP.NET Web 项目(1):概述 - 创建 Web 应用程序项目

    注:本文是“在 Visual Studio 2013 中创建 ASP.NET Web 项目”专题的一部分,详情参见 专题导航 . 预备知识 本专题适用于 Visual Studio 2013 及以上版 ...

  3. 【转载】在 Visual Studio 2012 中创建 ASP.Net Web Service

    在 Visual Studio 2012 中创建 ASP.Net Web Service,步骤非常简单.如下: 第一步:创建一个“ASP.Net Empty Web Application”项目 创建 ...

  4. Visual Studio 2010中创建ASP.Net Web Service

    转自:http://blog.csdn.net/xinyaping/article/details/7331375 很多人在论坛里说,在Visual Studio 2010中不能创建“ASP.Net ...

  5. 在 Visual Studio 2010 中创建 ASP.Net Web Service

    第一步:创建一个“ASP.Net Empty Web Application”项目 第二步:在项目中添加“Web Service”新项目 第一步之后,Visual Studio 2010会创建一个仅含 ...

  6. (转)在 Visual Studio 2010 中创建 ASP.Net Web Service

    很多人在论坛里说,在Visual Studio 2010中不能创建“ASP.Net Web Service”这种project了,下面跟帖者云云,有的说这是因为微软已经将Web Service整合进W ...

  7. Visual Studio 2013 无法创建MVC项目,系统找不到指定的文件.(Exception from HRESULT:08x0070002)

    在Visual Studio 2013中创建新MVC项目,(PS:现在创建个MVC项目,差点都找不到在哪,汗!-) 确定后提示,系统找不到指定的文件.(Exception from HRESULT:0 ...

  8. 在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移

    在ASP.NET MVC5 及 Visual Studio 2013 中为Identity账户系统配置数据库链接及Code-First数据库迁移 最近发布的ASP.NET MVC 5 及Visual ...

  9. 在Visual Studio 2013 中使用C++单元测试

    本文主要介绍在Visual Studio 2013中对代码进行单元测试的方法,包含了两方面的内容:对已有的Dll文件进行单元测试,以及对已有的源文件进行单元测试. 1. VS2013对DLL文件的单元 ...

随机推荐

  1. BestCoder22 1003.NPY and shot 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5144 题目意思:有个人抛物体,已知抛的速度和高度,问可以抛到的最远距离是多少.即水平距离. 做的时候是 ...

  2. codeforces 490B.Queue 解题报告

    题目链接:http://codeforces.com/problemset/problem/490/B 题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi.注意,排在第一个位 ...

  3. 微信video标签全屏无法退出bug 本文系转载

    安卓(android)微信里面video播放视频,会被强制全屏,播放完毕后还有腾讯推荐的视频,非常讨厌..强制被全屏无法解决,但是视频播放完毕后退出播放器可以解决.方法就是视频播放完毕后,用音频aud ...

  4. C#中XmlTextWriter读写xml文件详细介绍(转)

    转自http://www.jb51.net/article/35230.htm   .NET中包含了很多支持XML的类,这些类使得程序员使用XML编程就如同理解XML文件一样简单.在这篇文章中,我将给 ...

  5. eclipse修改jdk后版本冲突问题

    将安装的jdk1.8改为1.7之后出现了很淡疼的问题 修改工程下.setting/ org.eclipse.jdt.core.prefs eclipse.preferences.version=1or ...

  6. HDU 5996 dingyeye loves stone ---BestCoder Round #90

    题目链接 设根节点的深度为0,将所有深度为奇数的节点的石子数目xor起来,则先手必胜当且仅当这个xor和不为0. 证明同阶梯博弈.对于偶深度的点上的石子,若对手移动它们,则可模仿操作:对于奇深度上的石 ...

  7. 【el表达式】jsp中设置默认图像

    <img alt="头像" src="${empty members.headPic ?'images/icon.png':members.headPic}&quo ...

  8. 20145213《Java程序设计》实验报告一:Java开发环境的熟悉(Windows+IDEA)

    20145213<Java程序设计>实验报告一:Java开发环境的熟悉(Windows+IDEA) 实验要求 使用JDK编译.运行简单的Java程序. 使用IDEA编辑.编译.运行.调试J ...

  9. August 13th 2016 Week 33rd Saturday

    What makes life dreary is the want of motive. 没有目标与动力,生活便会郁闷无光. Without dreams and hope, there will ...

  10. 简易qq对话框

    //本程序由QT5 creator编译可运行 //dialog.h 1 #ifndef DIALOG_H #define DIALOG_H #include <QDialog> class ...