Created
December 29, 2011 02:04
-
-
Save ishisaka/1531152 to your computer and use it in GitHub Desktop.
@chackさんのサンプルをModelからデータを取得するようにしてPOSTコマンドを追加
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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