I'm developing an ASP.NET Web Api with C# and .NET Framework 4.5.1.
我正在使用C#和.NET Framework 4.5.1开发ASP.NET Web Api。
I have an strange behaviour. I can access the two following routes but I can't reach the third and the fourth. I get the error:
我有一个奇怪的行为。我可以访问以下两个路线,但我无法到达第三个和第四个路线。我收到错误:
No HTTP resource was found that matches the request URI. No action was found in the 'ExternalCodes' driver match the application.
未找到与请求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);
}
All are in the same class.
所有人都在同一个班级。
This is my WebApiConfig
class:
这是我的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" });
}
}
These are my nuget packages:
这些是我的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>
This is very strange, because all routes are in the same controller.
这很奇怪,因为所有路由都在同一个控制器中。
Any idea what is happening?
知道发生了什么吗?
The links to access the actions are:
访问操作的链接是:
http://mySpecialServer:53827/api/ExternalCodes/SetBatchCodesAsUsed
http://mySpecialServer:53827/api/ExternalCodes/SetCodesAsUsed
1 个解决方案
#1
You are accessing the action on your controller without parameter.
您正在访问控制器上没有参数的操作。
In your call the parameter codes
is null. This is working because the List<string>
-datatype is nullable.
在您的调用中,参数代码为空。这是有效的,因为List
[HttpPut]
[Route("api/ExternalCodes/SetCodesAsUsed")]
public HttpResponseMessage SetCodesAsUsed(List<string> codes)
{
return ChangeCodesStatus(codes, 3);
}
The second link does the same -> batchId is null. But int is a non-nullable type so you get an error.
第二个链接也是一样 - > batchId为null。但是int是一个不可为空的类型,所以你得到一个错误。
Anyway you should change your route if you have parameters (especially if they are non-nullable).
无论如何,如果你有参数,你应该改变你的路线(特别是如果它们是不可空的)。
[Route("api/ExternalCodes/SetBatchCodesAsUnUsed/{batchId}")]
This route expects an id as a part of your call. This has the advantage, that you always know, which resource you are changing.
此路线期望将id作为您通话的一部分。这样做的好处是,您始终知道要更改的资源。
The call would look like this:
电话会是这样的:
http://mySpecialServer:53827/api/ExternalCodes/SetBatchCodesAsUsed/1
#1
You are accessing the action on your controller without parameter.
您正在访问控制器上没有参数的操作。
In your call the parameter codes
is null. This is working because the List<string>
-datatype is nullable.
在您的调用中,参数代码为空。这是有效的,因为List
[HttpPut]
[Route("api/ExternalCodes/SetCodesAsUsed")]
public HttpResponseMessage SetCodesAsUsed(List<string> codes)
{
return ChangeCodesStatus(codes, 3);
}
The second link does the same -> batchId is null. But int is a non-nullable type so you get an error.
第二个链接也是一样 - > batchId为null。但是int是一个不可为空的类型,所以你得到一个错误。
Anyway you should change your route if you have parameters (especially if they are non-nullable).
无论如何,如果你有参数,你应该改变你的路线(特别是如果它们是不可空的)。
[Route("api/ExternalCodes/SetBatchCodesAsUnUsed/{batchId}")]
This route expects an id as a part of your call. This has the advantage, that you always know, which resource you are changing.
此路线期望将id作为您通话的一部分。这样做的好处是,您始终知道要更改的资源。
The call would look like this:
电话会是这样的:
http://mySpecialServer:53827/api/ExternalCodes/SetBatchCodesAsUsed/1