// Controllers.cs
namespace Microshaoft.WebApi.Controllers
{
using Microshaoft.WebApi.Models;
using System;
using System.Collections.Generic;
using System.Web.Http;
using System.Net.Http;
using System.Net;
using System.Linq;
public class PersonsController : ApiController
{
List<Person> _persons = new Person[]
{
new Person("张栢芝", 71, 178, 49)
, new Person("章子怡", 23, 177, 33)
, new Person("周 迅", 12, 180, 80)
, new Person("徐静蕾", 12, 150, 70)
, new Person("赵 薇", 23, 166, 60)
, new Person("宋丹丹", 50, 183, 50)
, new Person("翠花儿", 23, 177, 34)
, new Person("赵丽蓉", 50, 184, 40)
, new Person("郭晶晶", 50, 184, 41)
}.ToList();
public IEnumerable<Person> GetXXX()
{
return _persons;
}
public IEnumerable<Person> getXXXX(int i)
{
return _persons;
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
public HttpResponseMessage Post(Person item)
{
_persons.Add(item);
var response = Request.CreateResponse<Person>(HttpStatusCode.Created, item);
string uri = Url.Link("DefaultApi", new { Name = item.Name });
response.Headers.Location = new Uri(uri);
return response;
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
public void Put(int id, Person item)
{
_persons[id] = item;
}
// DELETE api/values/5
public void Delete(int id)
{
_persons.RemoveAt(id);
}
}
}
// Models.cs
namespace Microshaoft.WebApi.Models
{
using System;
public class Person : IComparable<Person>
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public int Height
{
get;
set;
}
public int Weight
{
get;
set;
}
public Person(string name, int age, int height, int weight)
{
Name = name;
Age = age;
Height = height;
Weight = weight;
}
public Person()
{
}
public override string ToString()
{
return
string.Format
(
"姓名:{0}, 年龄:{1:N}, 体重:{2:N}, 身高:{3:N}"
, Name
, Age
, Height
, Weight
);
}
public int CompareTo(Person other)
{
int r = 0;
r = Age - other.Age;
if (r == 0)
{
r = Height - other.Height;
if (r == 0)
{
r = Weight - other.Weight;
}
}
return r;
}
}
}
// BundleConfig.cs
namespace Microshaoft.WebMvc
{
using System.Web.Optimization;
public class BundleConfig
{
// For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add
(
new ScriptBundle("~/bundles/jquery")
.Include("~/Scripts/jquery-{version}.js")
);
bundles.Add
(
new ScriptBundle("~/bundles/jqueryui")
.Include("~/Scripts/jquery-ui-{version}.js")
);
bundles.Add
(
new ScriptBundle("~/bundles/jqueryval")
.Include
(
"~/Scripts/jquery.unobtrusive*"
, "~/Scripts/jquery.validate*"
)
);
// Use the development version of Modernizr to develop with and learn from. Then, when you're
// ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
bundles.Add
(
new ScriptBundle("~/bundles/modernizr")
.Include("~/Scripts/modernizr-*")
);
bundles.Add
(
new StyleBundle("~/Content/css")
.Include("~/Content/site.css")
);
bundles.Add
(
new StyleBundle("~/Content/themes/base/css")
.Include
(
"~/Content/themes/base/jquery.ui.core.css",
"~/Content/themes/base/jquery.ui.resizable.css",
"~/Content/themes/base/jquery.ui.selectable.css",
"~/Content/themes/base/jquery.ui.accordion.css",
"~/Content/themes/base/jquery.ui.autocomplete.css",
"~/Content/themes/base/jquery.ui.button.css",
"~/Content/themes/base/jquery.ui.dialog.css",
"~/Content/themes/base/jquery.ui.slider.css",
"~/Content/themes/base/jquery.ui.tabs.css",
"~/Content/themes/base/jquery.ui.datepicker.css",
"~/Content/themes/base/jquery.ui.progressbar.css",
"~/Content/themes/base/jquery.ui.theme.css"
)
);
}
}
}
// FilterConfig.cs
namespace Microshaoft.WebMvc
{
using System.Web.Mvc;
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
}
// RouteConfig.cs
namespace Microshaoft.WebMvc
{
using System.Web.Mvc;
using System.Web.Routing;
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute
(
name :
"Default",
url :
"{controller}/{action}/{id}",
defaults :
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
}
}
}
// WebApiConfig.cs
namespace Microshaoft.WebApi
{
using System.Web.Http;
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute
(
name :
"DefaultApi",
routeTemplate :
"services/restful/api/{controller}/{id}",
defaults :
new
{
id = RouteParameter.Optional
}
);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
}
}
// Global.asax.cs
// Global.asax
/*
<%@ Application Language="C#" Inherits="Microshaoft.Web.Global" %>
*/
namespace Microshaoft.Web
{
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Microshaoft.WebMvc;
using Microshaoft.WebApi;
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class Global : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
// comment for Web API
//BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}

ASP.NET MVC 4 WebAPI Simple Sample的更多相关文章

  1. 给Asp.Net MVC及WebApi添加路由优先级

    一.为什么需要路由优先级 大家都知道我们在Asp.Net MVC项目或WebApi项目中注册路由是没有优先级的,当项目比较大.或有多个区域.或多个Web项目.或采用插件式框架开发时,我们的路由注册很可 ...

  2. AJAX跨域调用ASP.NET MVC或者WebAPI服务

    关于AJAX跨域调用ASP.NET MVC或者WebAPI服务的问题及解决方案 作者:陈希章 时间:2014-7-3 问题描述 当跨域(cross domain)调用ASP.NET MVC或者ASP. ...

  3. 【转载】为ASP.NET MVC及WebApi添加路由优先级

    路由方面的: 转载地址:http://www.jb51.net/article/73417.htm Author:lijiao 这是一个对Asp.Net Mvc的一个很小的功能拓展,小项目可能不太需要 ...

  4. AJAX跨域调用ASP.NET MVC或者WebAPI服务的解决方案

    问题描述 当跨域(cross domain)调用ASP.NET MVC或者ASP.NET Web API编写的服务时,会发生无法访问的情况. 重现方式 使用模板创建一个最简单的ASP.NET Web ...

  5. ASP.NET MVC对WebAPI接口操作(添加,更新和删除)

    昨天<怎样操作WebAPI接口(显示数据)>http://www.cnblogs.com/insus/p/5670401.html 既有使用jQuery,也有使作HttpClient来从数 ...

  6. 关于AJAX跨域调用ASP.NET MVC或者WebAPI服务的问题及解决方案

      作者:陈希章 时间:2014-7-3 问题描述 当跨域(cross domain)调用ASP.NET MVC或者ASP.NET Web API编写的服务时,会发生无法访问的情况. 重现方式 使用模 ...

  7. ASP.NET MVC 4 WebAPI. Support Areas in HttpControllerSelector

    This article was written for ASP.NET MVC 4 RC (Release Candidate). If you are still using Beta versi ...

  8. Asp.Net MVC part6 WebAPI

    两种web服务SOAP风格:基于方法,产品是WebServiceREST风格:基于资源,产品是WebAPI可以返回json.xml类型的数据对于数据的增.删.改.查,提供相对的资源操作,按照请求的类型 ...

  9. 如何解决Asp.Net MVC和WebAPI的Controller名称不能相同的问题

    1.问题描述 假如有一个文章的业务(Article),我们在 Controllers文件夹中创建MVC Controller和Api Controller,各个Controller中都有相同的获取文章 ...

随机推荐

  1. VS中计算程序运行时间

    VS中计算程序运行的时间   http://bbs.csdn.net/topics/39068881 有时候在设计程序完了之后需要计算程序运行的时间. 这时候可以使用Windows的库函数 GetIi ...

  2. python 3.5.2安装mysql驱动报错

    python 3.5.2安装mysql驱动报错 python 3.5.2安装mysql驱动时出现如下异常: [root@localhost www]# pip install mysql-connec ...

  3. Java中使用Collections.sort()方法对数字和字符串泛型的LIst进行排序

    在List的排序中常用的是Collections.sort()方法,可以对String类型和Integer类型泛型的List集合进行排序. 首先演示sort()方法对Integer类型泛型的List排 ...

  4. ios 中使用https的知识

    先看文章,这篇文章说的是使用AFNetworing进行https时的事项,十分好!http://blog.cnbang.net/tech/2416/ ios中使用https,主要就是使用NSURLCr ...

  5. 7.js模式-装饰者模式

    1. 装饰者模式 给对象动态增加职责的方式称为装饰者模式. Function.prototype.before = function(beforefn){ var _self = this; retu ...

  6. Lock+Condition 相对于 wait+notify 的一个优势案例分析

    问题的描述 启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18, ...

  7. 用原生DOM 遍历页面节点

    代码丢失,直接上图:

  8. 仿知乎程序 fragment的切换以及toolbar在不同页面下显示的menu不同

           我们在看知乎的时候,你会发现,首页,发现,关注,收藏,草稿这五项,你在点击之后进入到相应页面之后,侧滑菜单还在,你左侧滑一下,这个侧滑菜单还在,而提问,左滑屏幕,这个页面就没有,有点像返 ...

  9. HDU 5879 Cure -2016 ICPC 青岛赛区网络赛

    题目链接 题意:给定一个数n,求1到n中的每一项的平方分之一的累加和. 题解:题目没有给数据范围,而实际上n很大很大超过long long.因为题目只要求输出五位小数,我们发现当数大到一定程度时值是固 ...

  10. September 29th 2016 Week 40th Thursday

    Prosperity discovers vice, adversity virtue. 得意时露瑕疵,逆境中见品质. I wish I would have someone like you, fr ...