无法点击特定的Get方法...错误404未找到

时间:2012-10-25 08:41:08

标签: c# asp.net-web-api

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

我能够点击前两个获取方法,但是当我点击“格式”方法时,我得到404未找到错误

客户端应用程序调用

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

路线配置

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

请告诉我哪里出错了?

2 个答案:

答案 0 :(得分:1)

由于参数'postedFormat'的名称,它与您的路线参数名称'id'不匹配。尝试添加指定postedFormat作为最后一个参数的路由。

答案 1 :(得分:0)

快速解决方案将是@Justin Harvey,建议。即使你成为方法

来自这个

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

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

这是因为默认路线:routeTemplate: "api/{controller}/{action}/{id}",接受id

您可以更改路线以接受postingFormat作为输入参数。