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

I am able to hit the first two get methods but when i hit "format" method am getting a 404 Not found error

Client App call

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

Route Config

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

Please let me know where am going wrong?

4

2 回答 2

1

It is because of the name of the parameter 'postedFormat', it will not match with your route paramter name 'id'. Try adding a route that specifies postedFormat as the last parameter.

于 2012-10-25T08:44:45.640 回答
0

A quick fix would be as @Justin Harvey, suggested. i.e to make you method

from this

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

to

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

this is because the default route : routeTemplate: "api/{controller}/{action}/{id}", accepts an id.

You can change your route to accept postedFormat as input parameter.

于 2012-10-25T12:28:42.850 回答