ASP.NET MVC 4 WebAPI Simple Sample


// 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);
        }
    }
}

posted @ 2013-07-11 02:08  于斯人也  阅读(811)  评论(0编辑  收藏  举报