1、创建项目
只是单纯的使用Web API的功能,而不需要使用的MVC,这个时候就该抛开MVC来新建项目了。
首先,新建一个Asp.Net空应用程序,在程序集中添加引用System.Web.Http和System.Web.Http.WebHost。
2、添加引用
继续添加System.Net.Http
另外还需要引用Json.Net,可以通过Nuget或者直接用下载好的dll
3、添加路由映射
添加路由映射,创建App_Start文件夹,新建WebApiConfig.cs文件,添加如下路由配置:
namespace WebAPIDemo2
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
4、配置Global文件
新建Global.acax文件,在Application_Start中调用完成注册
namespace WebAPIDemo2
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
}
}
5、创建控制器
创建WebAPI控制器
在项目中添加Model到Models文件夹当中
BookChapter.cs
namespace WebAPIDemo2.Models
{
public class BookChapter
{
///
/// 数量
///
public int Number { get; set; }
///
/// 标题
///
public string Title { get; set; }
///
/// 页数
///
public int Pages { get; set; }
}
}
Book.cs
using System.Collections.Generic;
using System.Linq;
namespace WebAPIDemo2.Models
{
public class Book
{
public Book(int id, string title, params BookChapter[] chapters)
{
this.Id = id;
this.Title = title;
this.BookChapters = chapters.ToList();
}
public int Id { get; private set; }
public string Title { get; private set; }
public ICollection BookChapters { get; private set; }
}
}
在项目中新建控制器
BookChaptersController.cs
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WebAPIDemo2.Models;
namespace WebAPIDemo2.Controllers
{
public class BookChaptersController : ApiController
{
// GET api/bookchapters
[HttpGet]
public IEnumerable Get()
{
return chapters;
}
// GET api/bookchapters/5
public BookChapter Get(int id)
{
return chapters.Where(c => c.Number == id).SingleOrDefault();
}
// POST api/bookchapters
[AcceptVerbs("POST")]
[ActionName("bookchapter")]
public void PostBookChapter([FromBody]BookChapter value)
{
chapters.Add(value);
}
// PUT api/bookchapters/5
public void PutBookChapter(int id, [FromBody]BookChapter value)
{
chapters.Remove(chapters.Where(c => c.Number == id).Single());
chapters.Add(value);
}
// DELETE api/bookchapters/5
public void DeleteBookChapter(int id)
{
chapters.Remove(chapters.Where(c => c.Number == id).Single());
}
private static List chapters;
static BookChaptersController()
{
chapters = new List()
{
new BookChapter{Number=1,Title=".Net Architecture",Pages=20},
new BookChapter{Number=2,Title=".Net ",Pages=120},
new BookChapter{Number=3,Title=".Net a",Pages=480},
new BookChapter{Number=4,Title=".Net b",Pages=200},
new BookChapter{Number=5,Title=".Net c",Pages=210},
new BookChapter{Number=17,Title=".Net d",Pages=230},
new BookChapter{Number=42,Title=".Net e",Pages=270},
new BookChapter{Number=43,Title=".Net f",Pages=320},
new BookChapter{Number=50,Title=".Net g",Pages=520}
};
}
}
}
BookChaptersAttrController.cs
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WebAPIDemo2.Models;
namespace WebAPIDemo2.Controllers
{
//添加路由前缀
[RoutePrefix("booksamples")]
public class BookChaptersAttrController : ApiController
{
private static List chapters;
private static List books;
static BookChaptersAttrController()
{
chapters = new List
{
new BookChapter{Number=1,Title=".Net Architecture",Pages=20},
new BookChapter{Number=2,Title=".Net ",Pages=120},
new BookChapter{Number=3,Title=".Net a",Pages=480},
new BookChapter{Number=4,Title=".Net b",Pages=200},
new BookChapter{Number=5,Title=".Net c",Pages=210},
new BookChapter{Number=17,Title=".Net d",Pages=230},
new BookChapter{Number=42,Title=".Net e",Pages=270},
new BookChapter{Number=43,Title=".Net f",Pages=320},
new BookChapter{Number=50,Title=".Net g",Pages=520}
};
books = new List()
{
new Book(1, "Professional C#", chapters.ToArray()),
new Book(2, "Professional Asp.Net MVC 4")
};
}
//单参数路由
[Route("books/{bookId}")]//URL:http://server/books/2
public IEnumerable GetBookChapters(int bookId)
{
return books.Where(b => b.Id == bookId).Single().BookChapters;
}
//多参数路由
[Route("books/{bookId}/chapters/{chapterId}")]//URL:http://server/books/2/chapters/17
//类型限制参数
//[Route("books/{bookId:int}/chapters/{chapterId:int}")]
public BookChapter GetBookChapter(int bookId, int chapterId)
{
return
books.Single(b => b.Id == bookId)
.BookChapters.SingleOrDefault(c => c.Number == chapterId);
}
}
}
6、项目结构如下: