WEB API 2路由问题

时间:2015-07-29 19:42:07

标签: asp.net-mvc asp.net-web-api-routing

" message":"未找到与请求URI匹配的HTTP资源' http://localhost:64221/api/companies/lncr/Orders/00029303_070/Lines/6'。",

" messageDetail":"未在控制器上找到任何操作' PurchLines'与请求匹配。"

[RoutePrefix("api/companies/{compId}/Orders/{orderId}/Lines")]
public class PurchLinesController : BaseApiController
{
    public PurchLinesController(IPortalReposotry repo)
        : base(repo)
    {

    }

    [Route("")]
    public HttpResponseMessage Get(String compId, String orderId, int levelOfDetails = 1)
    {
        IQueryable<PurchLine> query;

        if (levelOfDetails == 1)
        {
            query = PortalReposotry.GetPurchasLines(compId, orderId);
        }
        else
        {
            query = PortalReposotry.GetPurchasLinesWithApprovalHist(compId, orderId);
        }

        if (query.Count() == 0)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

        var results = query.ToList()
                           .Select(v => this.ModelFactory.Create(v));


        return Request.CreateResponse(HttpStatusCode.OK, results);
    }

    [Route("{lineId:double}" , Name="line")]
    public HttpResponseMessage Get(String compId, String purchId, double lineId, int levelOfDetails = 1)
    {
        PurchLine results;
        if (levelOfDetails == 1)
        {
            results = PortalReposotry.GetPurchasLine(compId, purchId, lineId);
        }
        else
        {
            results = PortalReposotry.GetPurchasLineWithApprovalHist(compId, purchId, lineId);
        }

        if (results == null)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

        return Request.CreateResponse(HttpStatusCode.OK, ModelFactory.Create(results));
    }
}

请求应与第二个Get请求匹配 的 API /公司/ {compId} /订单/ {订单ID} /线/ {lineId} http://localhost:64221/api/companies/lncr/Orders/00029303_070/Lines/6

lineId是double的一种,我也试过了 http://localhost:64221/api/companies/lncr/Orders/00029303_070/Lines/6.0/

但那也不起作用,所以我在这里不知所措。

1 个答案:

答案 0 :(得分:0)

我发现了我的错误,第二个获取响应有purchId但我的RoutePrefix被定义为orderId。容易疏忽。