我在Chrome Postman中发出了一个GET请求。它看起来如下:
http://localhost/WCAPI/Lookup/WCClassDesc/State/AL/Class/7230/DescCode/00/EffDate/2016-04-13
有人明白我为什么会在《邮差》上得到这样的回应吗?
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost/WCAPI/Lookup/WCClassDesc/State/AL/Class/7230/DescCode/00/EffDate/2016-04-13'.",
"MessageDetail": "No action was found on the controller 'WCClassDesc' that matches the request."
}
我的代码是:
using System;
using System.Web.Http;
/// <summary>
/// API for loading WCClassDescription which is shown on the PremByClass page.
namespace WCAPI.Controllers.Lookup {
[RoutePrefix("Lookup/WCClassDesc")]
public class WCClassDescController : ApiController {
[Route("State/{State}/Class/{Class}/DescCode/{DescCode}/EffDate/{EffDate}")]
public Models.Lookup.WCClassDesc Get(string ClassState, string ClassCode, string DescCode, DateTime EffDate) {
var desc = (new Premium.BLL.WCClassDesc()).GetCurrentWCClassDesc(ClassState, ClassCode, DescCode, EffDate);
var WC = AutoMapper.Mapper.Map<Models.Lookup.WCClassDesc>(desc);
return WC;
}
}
}
如有任何帮助或指示,我们将不胜感激。
Jason
您的代码有多个问题,让我们从URI:开始
假设您使用主机的根路径(localhost)测试应用程序,并遵循RoutePrefix
和Route
属性的定义,则资源的正确URI如下:
http://localhost/Lookup/WCClassDesc/State/AL/Class/7230/DescCode/00/EffDate/2016-04-13
这是因为在RoutePrefix
属性中没有定义WCAPI
。
另一个问题与路由参数映射有关,您将参数定义为{State}
和{Class}
,但随后您在方法中要求ClassState
和ClassCode
。
重命名这些参数以匹配路由中定义的参数,否则Web API将不会将它们映射到方法参数。