0

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;
    }

誰かが私に正しい方向へのプッシュを与えることができます;-)

前もって感謝します

アンダース・ペダーセン

4

2 に答える 2

0

クラスの名前がVacationControllerであると仮定すると、これらのメソッドのデフォルト ルートは次のようになります。

 /api/Vacation/GetVacation
 /api/Vacation/GetVacation?id=1
 /api/Vacation/ByThemeID?id=1

これはすべて、ルーティングが更新されていないことを前提としています。

于 2013-10-28T11:14:40.440 に答える
0

defaults action = "GetVacation" を追加し、id をオプションにします

ApiController 基本クラスは、オーバーロードGetVacation()GetVacation(int id)選択を自動的に処理できます。

WebApiConfigを登録する

    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{*param}",
            defaults: new { action = "Get", param = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "Vacation",
            routeTemplate: "api/vacation/{action}/{*id}",
            defaults: new { controller = "Vacation", action = "GetVacation", id = RouteParameter.Optional }
        );
    }
于 2013-10-28T12:20:58.527 に答える