0
[HttpGet]
    [ActionName("all")]
    public HttpResponseMessage GetAllCompetitions()
    {
        return Request.CreateResponse(HttpStatusCode.OK, Repository.FindAll());
    }

    [HttpGet]
    [ActionName("GetCompetition")]
    public HttpResponseMessage GetCompetitionById(long id)
    {
        Competition competition = Repository.FindById(id);
        if (competition == null)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }
        return Request.CreateResponse(HttpStatusCode.OK, competition);
    }

    [HttpGet]
    [ActionName("format")]
    public HttpResponseMessage format(string postedFormat)
    {
        CompetitionMediaFormat format = (CompetitionMediaFormat)Enum.Parse(typeof(CompetitionMediaFormat), postedFormat, true);

        return Request.CreateResponse(HttpStatusCode.OK, Repository.FindByFormat(format));
    }

最初の 2 つの get メソッドをヒットすることはできますが、「format」メソッドをヒットすると 404 Not found エラーが発生します

クライアント アプリの呼び出し

var response = await client.GetAsync("api/Competition/format/music");

ルート構成

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

どこが間違っているのか教えてください。

4

2 に答える 2

1

これは、パラメータ「postedFormat」の名前が原因で、ルート パラメータ名「id」と一致しません。postedFormat を最後のパラメーターとして指定するルートを追加してみてください。

于 2012-10-25T08:44:45.640 に答える
0

@Justin Harveyが提案したように、簡単な修正があります。つまり、あなたを方法にする

これから

public HttpResponseMessage format(string postedFormat)
{...}

public HttpResponseMessage format(string id)
{...}

これは、デフォルトのルート :routeTemplate: "api/{controller}/{action}/{id}",id.

ルートを変更して、postedFormat を入力パラメーターとして受け入れることができます。

于 2012-10-25T12:28:42.850 に答える