ASP.NET Web API プロジェクトには、これらのアクション メソッドを使用する VacationController があります。これを達成するためのルートを構築するにはどうすればよいですか?
public Enumerable<Vacation> GetVacation()
{
// Get all vactions
return vacations;
}
public Vacation GetVacation(int id)
{
// Get one vaction
return vacation;
}
public Enumerable<Vacation> ByThemeID(int themeID)
{
// Get all vactions by ThemeID
return vacations;
}
URLはこんな感じでお願いします
/api/vacation // All vacations
/api/vacation/5 // One vacation
/api/vacation/ByThemeID/5 // All vacations from one theme
2013 年 10 月 30 日を編集
Pasit R ルートを試してみましたが、仕事に就けません。思いつく限りの組み合わせを試してみました。
これは私が知っていることです。ご覧のとおり、ルートの先頭に追加のパラメーターを追加しました。さまざまなレーベルで販売されているバケーションを分離するために、それが必要であることに気付きました。
私が使っているルートはこちらです。これらの URL の動作は問題ありません
/api/vacation // All vacations
/api/vacation/5 // One vacation
/api/vacation/ByThemeID/5 // All vacations from one theme
ただし、最後のURLでは機能しません
config.Routes.MapHttpRoute(
name: "DefaultApiSimbo",
routeTemplate: "api/{label}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
ここで、VacationController の Action メソッド
// ByThemeID api/{label}/Vacation/ByThemeId/{id}
[HttpGet]
public IEnumerable<Vacation> ByThemeID(string label, int id)
{
return this.repository.Get(label);
}
// GET api/{label}/Vacation
public IEnumerable<Vacation> GetVacation(string label)
{
return repository.Get(label);
}
// GET api/{label}/Vacation/{id}
public Vacation GetVacation(string label, int id)
{
Vacation vacation;
if (!repository.TryGet(label, id, out vacation))
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
return vacation;
}
誰かが私に正しい方向へのプッシュを与えることができます;-)
前もって感謝します
アンダース・ペダーセン