我的控制器中有代码,如下所示:
[Route("api/deliveryitems/InsertIntoPPTData/{stringifiedRecord}")]
Run Code Online (Sandbox Code Playgroud)
......我正在通过Postman这样称呼它:
http://localhost:21609/api/deliveryitems/InsertIntoPPTData?
stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2
Run Code Online (Sandbox Code Playgroud)
......但得到:
{
Message: "No HTTP resource was found that matches the request URI
'http://localhost:21609/api/deliveryitems/InsertIntoPPTData? stringifiedRecord=serNum77;tx2;siteNum2;bla2.xml;ppt_user2;tx_memo2;file_beg2;file_end2'."
MessageDetail: "No type was found that matches the controller named 'deliveryitems'."
}
Run Code Online (Sandbox Code Playgroud)
以相同方式创建和调用的其他REST方法也很好 - 为什么不是这个?与其他人没有共同点的唯一事情是它是一个HttpPost,而其他的是HttpGet.这有什么不同吗?我正在从邮差下拉试图调用此方法REST时选择"邮报".
是的,它显然与它是一个Post并在URI中传递args无关,因为我现在使用HttpGet方法获得相同的东西:
{
Message: "No HTTP resource was found that matches the request URI
'http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12'."
MessageDetail: "No action was found on the controller 'Department' that matches the request."
}
Run Code Online (Sandbox Code Playgroud)
我从邮递员这样称呼它:
http://localhost:21609/api/department/getndeptsfromid?firstId=2&countToFetch=12
Run Code Online (Sandbox Code Playgroud)
......它确实出现在我的控制器中:
[HttpGet]
[Route("api/department/getndeptsfromid/{firstId}/{countToFetch}")]
public List<Department> GetNDepartmentsFromID(int FirstId, int CountToFetch)
{
return CCRService.GetNDepartmentsFromID(FirstId, …Run Code Online (Sandbox Code Playgroud) 我试图将Web API添加到现有的ASP.NET MVC项目中。这最初是一个ASP.NET MVC 2网站,后来使用以下过程将其升级到MVC 3,然后再升级到MVC 4:
将ASP.NET MVC 2项目升级到ASP.NET MVC 3
将ASP.NET MVC 3项目升级到ASP.NET MVC 4
我的Api控制器非常简单。实际上,这是默认模板:
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了一个类来注册de API路由。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
从Application_Start调用 …