MVC WebApi

两种web服务
SOAP风格:基于方法,产品是WebService
REST风格:基于资源,产品是WebAPI

对于数据的增、删、改、查,提供相对的资源操作,按照请求的类型进行相应处理,主要包括Get()Post()Put()Delete(),这些都是http协议支持的请求方式

APIController中定义crud的方法,名称可以自定义,如果对应相应的资源操作,可以使用特性约束
主要的特性包括
HttpGet
HttpPost
HttpPut
HttpDelete

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;


namespace WebApiExam.Controllers
{
    public class UserInfoController : ApiController
    {
        // GET: api/UserInfo
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }


        // GET: api/UserInfo/5
        public string Get(int id)
        {
            return "value";
        }


        // POST: api/UserInfo
        public void Post([FromBody]string value)
        {
        }


        // PUT: api/UserInfo/5
        public void Put(int id, [FromBody]string value)
        {
        }


        // DELETE: api/UserInfo/5
        public void Delete(int id)
        {
        }
    }
}
原文地址:https://www.cnblogs.com/dxmfans/p/9434744.html