在很多跨平台的应用中就需要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. Python:IDLE清屏

    清屏很简单,为IDLE增加一个清屏的扩展ClearWindow即可. 首先下载clearwindow.py(点击可直接下载,不能下载的可以右键保存,格式为py结尾), 将这个文件放到Python安装目 ...

  2. 自定义tld标签,页面使用

    背景需求: 系统本身的session不能在页面使用 如下: controller: @RequestMapping(method=RequestMethod.GET) public String ge ...

  3. C# 对象操作

    //********************************************************************************* //************** ...

  4. C# 对象深度拷贝

    转载 using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using ...

  5. springMVC创建基础变量

    在springMVC中有一些变量是基础变量,可以在全局多个地方使用,在修改规则的时候,这样只用修改一个地方就好了,而且可以避免很多不必要的bug出现下面就来总结一下在我的项目中如何去创建一个全局基础变 ...

  6. ASP.NET MVC中解决日志并发处理log4net

    本章主要内容是将异常信息写到队列中,然后通过线程写到文本文件中,速度非常快,没有阻塞和延迟加载 1.首先在Model中建一个类MyExceptionAttribute.cs public class ...

  7. 原始套接字SOCK_RAW

    原始套接字SOCK_RAW 实际上,我们常用的网络编程都是在应用层的报文的收发操作,也就是大多数程序员接触到的流式套接字(SOCK_STREAM)和数据包式套接字(SOCK_DGRAM).而这些数据包 ...

  8. strlen与sizeof

    strlen计算不包括终止符null字节的字符串长度,而sizeof则计算包括终止null字节的长度.另一个差别,strlen需要一次函数调用,而sizeof在编译时计算缓冲区长度.

  9. Gif图片制作

    gif图片是博客中展示项目效果的一种很好的方式,为我们的app制作一张gif图片并不复杂,录制屏幕采用系统自带的QuickTime Player,制作gif采用PicGIF软件.licecap软件更是 ...

  10. 判断图片的类型(图片是data类型 )

    + (NSString *)typeForImageData:(NSData *)data { uint8_t c; [data getBytes:&c length:1]; switch ( ...