Skip to content

Instantly share code, notes, and snippets.

@ishisaka
Created December 29, 2011 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishisaka/1531152 to your computer and use it in GitHub Desktop.
Save ishisaka/1531152 to your computer and use it in GitHub Desktop.
@chackさんのサンプルをModelからデータを取得するようにしてPOSTコマンドを追加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Web;
using SuperHero.Resources;
using Models = SuperHero.Models;
namespace SuperHero.APIs
{
/// <summary>
/// REST API
/// </summary>
[ServiceContract]
public class SuperHeroApi
{
/// <summary>
/// 基本的に@Chackさんのサンプルを変更
/// </summary>
/// <returns>Heroのリスト</returns>
[WebGet(UriTemplate = "")]
public IQueryable<Models.Hero> Get() {
List<Models.Hero> heroes;
using (var context = new Models.HeroContext()) {
heroes = context.Heros.ToList();
}
return heroes.AsQueryable();
}
/// <summary>
/// POSTコマンドの追加
/// </summary>
/// <param name="heroes">Heroのリスト</param>
[WebInvoke(UriTemplate = "", Method = "POST")]
public void Add(IEnumerable<Models.Hero> heroes) {
using (var context = new Models.HeroContext()) {
foreach (var hero in heroes) {
context.Heros.Add(hero);
}
context.SaveChanges();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment