我正在使用C#和.NET Framework 4.5.1开发ASP.NET Web Api。
我有一种奇怪的行为。我可以访问以下两个路线,但我无法到达第三个和第四个路线。我收到错误:
找不到与请求URI匹配的HTTP资源。 “ExternalCodes”驱动程序中没有找到与应用程序匹配的操作。
[HttpPut]
[Route("api/ExternalCodes/SetCodesAsUsed")]
public HttpResponseMessage SetCodesAsUsed(List<string> codes)
{
return ChangeCodesStatus(codes, 3);
}
[HttpPut]
[Route("api/ExternalCodes/SetCodesAsUnUsed")]
public HttpResponseMessage SetCodesAsUnUsed(List<string> codes)
{
return ChangeCodesStatus(codes, 1);
}
[HttpPut]
[Route("api/ExternalCodes/SetBatchCodesAsUsed")]
public HttpResponseMessage SetBatchCodesAsUsed(int batchId)
{
return ChangeBatchCodesStatus(batchId, 3);
}
[HttpPut]
[Route("api/ExternalCodes/SetBatchCodesAsUnUsed")]
public HttpResponseMessage SetBatchCodesAsUnUsed(int batchId)
{
return ChangeBatchCodesStatus(batchId, 1);
}
所有人都在同一个班级。
这是我的WebApiConfig
课程:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ProductionOrderActionApi",
routeTemplate: "api/ProductionOrders/{action}/{orderNumber}",
defaults: new { controller = "ProductionOrders", orderNumber = RouteParameter.Optional });
config.Routes.MapHttpRoute(
name: "BatchesActionApi",
routeTemplate: "api/Batches/{action}",
defaults: new { controller = "Batches" });
config.Routes.MapHttpRoute(
name: "LinesActionApi",
routeTemplate: "api/Lines/{action}",
defaults: new { controller = "Lines" });
config.Routes.MapHttpRoute(
name: "ExternalCodesActionApi",
routeTemplate: "api/ExternalCodes",
defaults: new { controller = "ExternalCodes" });
config.Routes.MapHttpRoute(
name: "CodesActionApi",
routeTemplate: "api/Codes",
defaults: new { controller = "Codes" });
config.Routes.MapHttpRoute(
name: "AggregationsActionApi",
routeTemplate: "api/Aggregations",
defaults: new { controller = "Aggregations" });
}
}
这些是我的nuget包:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr" version="3.5.0.2" targetFramework="net451" />
<package id="bootstrap" version="3.3.2" targetFramework="net451" />
<package id="DotNetZip" version="1.9.3" targetFramework="net451" />
<package id="EntityFramework" version="6.1.3" targetFramework="net451" />
<package id="jQuery" version="2.1.3" targetFramework="net451" />
<package id="jQuery.Validation" version="1.13.1" targetFramework="net451" />
<package id="log4net" version="2.0.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Client.es" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.Core.es" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi.WebHost.es" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net451" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
<package id="Modernizr" version="2.8.3" targetFramework="net451" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
<package id="Ninject" version="3.2.2.0" targetFramework="net451" />
<package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net451" />
<package id="Ninject.Web.Common" version="3.2.3.0" targetFramework="net451" />
<package id="Ninject.Web.Common.WebHost" version="3.2.3.0" targetFramework="net451" />
<package id="WebActivatorEx" version="2.0.6" targetFramework="net451" />
<package id="WebGrease" version="1.6.0" targetFramework="net451" />
</packages>
这很奇怪,因为所有路径都在同一个控制器中。
知道发生了什么事吗?
访问操作的链接是:
http://mySpecialServer:53827/api/ExternalCodes/SetBatchCodesAsUsed
http://mySpecialServer:53827/api/ExternalCodes/SetCodesAsUsed
答案 0 :(得分:2)
您正在访问控制器上没有参数的操作。
在您的调用中,参数codes
为空。这是有效的,因为List<string>
- 数据类型是可以为空的。
[HttpPut]
[Route("api/ExternalCodes/SetCodesAsUsed")]
public HttpResponseMessage SetCodesAsUsed(List<string> codes)
{
return ChangeCodesStatus(codes, 3);
}
第二个链接也是如此 - &gt; batchId为null 。但是int是不可为空的类型,因此您会收到错误。
无论如何,如果您有参数(特别是如果它们不可为空),您应该更改路线。
[Route("api/ExternalCodes/SetBatchCodesAsUnUsed/{batchId}")]
此路线需要将ID作为通话的一部分。这样做的好处是,您始终知道要更改的资源。
电话会是这样的:
http://mySpecialServer:53827/api/ExternalCodes/SetBatchCodesAsUsed/1