简体   繁体   English

Web Api 2路由问题:“控制器上未找到任何操作......”

[英]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. 我遇到了在我的网络api路由上获得404的问题。 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. 虽然有很多关于这个问题的帖子,但它们似乎主要是关于改变路由的顺序,以便MVC路由不会覆盖api路由。

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 这是我的Web Api配置

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 正如你所看到的,我甚至试图评论mvc路线,看看是否有所作为

If I browse to http://localhost/api/paving-designer/get-forms/c6c489a7-46c6-420e-9e39-56797c8094cf 如果我浏览到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'. 没有找到与名为'paving-designer'的控制器匹配的类型。

I have tried changing the route prefix to the following but to no avail 我已经尝试将路由前缀更改为以下但无济于事

/api/paving-designer / API /铺路设计师
/paving-designer /铺路设计师
paving-designer 铺路设计师

And if I browse to http://localhost/api/pavingdesigner/get-forms/c6c489a7-46c6-420e-9e39-56797c8094cf 如果我浏览到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'. 找到了多个匹配名为'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 如果为此请求提供服务的路由('api / {controller} / {action} / {id}')发现多个控制器使用相同名称但名称空间不同而定义,则会发生这种情况

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. 但是使用它们中的第一个 - ControllerSelector无法找到paving-designerController。

Using the second - there is no action named get-forms. 使用第二个 - 没有名为get-forms的动作。 There are GetForms 有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. 好吧,在我的特定情况下,错误是由于我的IoC注册控制器两次。

This was causing the duplicate entries, which in turn made the attribute routing fail. 这导致重复的条目,这反过来使属性路由失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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