C# 尝试使用 WebAPI 时出现“未找到与名为“SampleSlashBaseService”的控制器匹配的类型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11384552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:41:03  来源:igfitidea点击:

Getting "No type was found that matches the controller named 'SampleSlashBaseService'" when trying to use WebAPI

c#.netasp.net-mvcasp.net-web-apiattributerouting

提问by azzlack

I have a webapi project with a base ApiController named SlashBaseService:

我有一个带有名为 SlashBaseService 的基本 ApiController 的 webapi 项目:

[RouteArea("uBase")]
public abstract class SlashBaseService : ApiController
{
}

The resulting dll is used in a WebForms project so I also have a WebActivator class with the following code to generate routes:

生成的 dll 用于 WebForms 项目,因此我还有一个 WebActivator 类,其中包含以下代码来生成路由:

RouteTable.Routes.MapHttpAttributeRoutes(config =>
{
    // Get all services inheriting from SlashBaseService
    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        foreach (var type in assembly.GetTypes())
        {
            if (type.IsSubclassOf(typeof(SlashBaseService)))
            {
                // Scan assembly
                config.ScanAssembly(assembly);

                // Skip the remaining types in this assembly
                break;
            }
        }
    }
});

RouteTable.Routes.MapHttpRoute(
    name: "DefaultBase",
    routeTemplate: "base/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional });

I also have a testservice in a separate assembly:

我在一个单独的程序集中也有一个测试服务:

public class SampleSlashBaseService : SlashBaseService
{
    [GET("TestOpenMethod")]
    public string GetTestOpenMethod()
    {
        return "Hello anonymous!";
    }

    [GET("Echo/{message}")]
    public string GetEcho(string message)
    {
        return message;
    }
}

All pretty simple stuff. The problem is when I try to go to one of the urls this generates i get the following message:

都是很简单的东西。问题是当我尝试转到生成的 URL 之一时,我收到以下消息:

No type was found that matches the controller named 'SampleSlashBaseService'.

未找到与名为“SampleSlashBaseService”的控制器匹配的类型。

The route list from /routes.axd also looks correct.

/routes.axd 中的路由列表看起来也正确。

采纳答案by azzlack

Found the problem.

发现问题了。

ApiControllersclass names need to be suffixed with "Controller", and mine was not. Changing it to SampleSlashBaseControllersolved the problem.

ApiControllers类名需要以“Controller”为后缀,而我的则没有。更改它以SampleSlashBaseController解决问题。

NOTE: It is possible to suffix it with "Service" as I did, but then you have to implement a custom IHttpControllerSelectorlike described here: http://netmvc.blogspot.no/2012/06/aspnet-mvc-4-webapi-support-areas-in.html

注意:可以像我一样使用“服务”作为后缀,但是您必须实现IHttpControllerSelector如下所述的自定义:http: //netmvc.blogspot.no/2012/06/aspnet-mvc-4-webapi- support-areas-in.html

回答by regisxp

In my case, there were two controllers with the same name, in different folders. Renaming them solved the problem instantly.

就我而言,在不同的文件夹中有两个具有相同名称的控制器。重命名它们立即解决了问题。

回答by Eranga

You also need to make sure the Controller class is Public

您还需要确保 Controller 类是Public

回答by mcanti

In my case the Controller was defined properly, but was not marked public.

在我的情况下,控制器被正确定义,但没有标记为公开。

回答by Adam S

For what it's worth, I ran into this and my problem was my manually-added class wasn't inheriting from ApiController. The class definition needs to be public class QueueController : ApiControllernot public class QueueController. What a dumb thing to overlook.

对于它的价值,我遇到了这个问题,我的问题是我手动添加的类不是从 ApiController 继承的。类定义public class QueueController : ApiController不需要public class QueueController。忽略了多么愚蠢的事情。