获得[否类型的发现命名为“SampleSlashBaseService'&QUOT控制器相匹配;尝试使用的WebAPI时 [英] Getting "No type was found that matches the controller named 'SampleSlashBaseService'" when trying to use WebAPI

查看:218
本文介绍了获得[否类型的发现命名为“SampleSlashBaseService'&QUOT控制器相匹配;尝试使用的WebAPI时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为SlashBaseService基地ApiController一个的WebAPI项目:

  [RouteArea(UBASE)] 
酒店的公共抽象类SlashBaseService:ApiController
{
}

结果DLL是在WebForms的项目中使用,所以我也有下面的代码WebActivator类来生成路线:

  RouteTable.Routes.MapHttpAttributeRoutes(配置=> 
{
//获取所有服务(在AppDomain.CurrentDomain.GetAssemblies()VAR组装)
{
的foreach从SlashBaseService
继承的foreach(VAR键入assembly.GetTypes())
{
如果(type.IsSubclassOf(typeof运算(SlashBaseService)))
{
//扫描组件,
config.ScanAssembly(组装);

//跳过本次大会
休息,其余类型;
}
}
}
});

RouteTable.Routes.MapHttpRoute(
的名字:DefaultBase,
routeTemplate:基地/ {}控制器/(编号),
默认:新{ ID = RouteParameter.Optional});



我也有一个单独的程序一TestService的:

 公共类SampleSlashBaseService:SlashBaseService 
{
[GET(TestOpenMethod)]
公共字符串GetTestOpenMethod()
{
返回匿名你好!;
}

[GET(回声/ {}消息)]
公共字符串GetEcho(字符串消息)
{
返回消息;
}
}



所有非常简单的东西。问题是,当我尝试去这会产生,我收到以下消息的其中一个网址:




没有型,发现匹配的控制器命名为'SampleSlashBaseService。




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


< DIV CLASS =h2_lin>解决方案

发现问题。



ApiControllers 类名称需要使用控制器作为后缀,而我却没有。
将其更改为 SampleSlashBaseController 解决了这个问题。



请注意:
是可能的以服务为我所做的,但你必须要实现自定义的 IHttpControllerSelector 喜欢这里描述的后缀是:的http://netmvc.blogspot.no/2012/06/aspnet-mvc-4-webapi-support-areas-in。 HTML


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

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

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:

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

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

解决方案

Found the problem.

ApiControllers class names need to be suffixed with "Controller", and mine was not. Changing it to SampleSlashBaseController solved the problem.

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

这篇关于获得[否类型的发现命名为“SampleSlashBaseService'&QUOT控制器相匹配;尝试使用的WebAPI时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆