简体   繁体   中英

Web Api 2 Routing Issue: “No action was found on the controller…”

I am having the issue of getting a 404 on my web api routing. Although there are many posts about this issue, they mainly seem to be about changing the order of the routes so that the MVC routes don't override the api routes.

I have tried all of the solution I have come across yet nothing seems to fix my issue.

Here is my controller:

[RoutePrefix("api/paving-designer")]
public class PavingDesignerController : ApiController
{
    [HttpGet]
    [Route("get-forms/{userId}")]
    public IHttpActionResult GetForms(Guid userId)
    {
        ICollection<PavingDesignerFlatForm> forms = _helper.GetForms(userId);

        if (forms != null)
        {
            return Ok(forms);
        }
        else
        {
            return NotFound();
        }
    }
}

And this is my Web Api Config

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new
            {
                id = RouteParameter.Optional
            });
    }
}

and this is my global asax

    private void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);

        // RouteConfig.RegisterRoutes(RouteTable.Routes);
        // Initialize Castle & install application components
        _bootstrapper = CastleInitialiser.Initialise();
    }

As you can see I have even tried to comment out the mvc routes to see if that made a difference

If I browse to http://localhost/api/paving-designer/get-forms/c6c489a7-46c6-420e-9e39-56797c8094cf

I get the following error:

No type was found that matches the controller named 'paving-designer'.

I have tried changing the route prefix to the following but to no avail

/api/paving-designer
/paving-designer
paving-designer

And if I browse to http://localhost/api/pavingdesigner/get-forms/c6c489a7-46c6-420e-9e39-56797c8094cf

I get the following error:

Multiple types were found that match the controller named 'pavingdesigner'. This can happen if the route that services this request ('api/{controller}/{action}/{id}') found multiple controllers defined with the same name but differing namespaces

I don't see how I can have multiple controllers as this is the only one I have.

Can anyone see where I am going wrong?

You are using both routing types.

Using attribute routing defined next route:

/api/paving-designer/get-forms/{userId}

Using default routing there is other route:

/api/{controller}/{action}/{id}

These routes are have the same template. But using the first of them - ControllerSelector can not find paving-designerController.

Using the second - there is no action named get-forms. There are GetForms

If you remove one of them - it should work.

Ok in my particular case, the error was being caused as my IoC was registering the controller twice.

This was causing the duplicate entries, which in turn made the attribute routing fail.

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