简体   繁体   中英

No action was found on controller

I'm developing an ASP.NET Web Api with C# and .NET Framework 4.5.1.

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.

[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:

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:

<?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

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 .

[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.

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.

The call would look like this:

http://mySpecialServer:53827/api/ExternalCodes/SetBatchCodesAsUsed/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM