我有一個名爲SlashBaseService基地ApiController一個的WebAPI項目時:獲得「無類型發現,名爲‘SampleSlashBaseService’控制器匹配」試圖使用的WebAPI
[RouteArea("uBase")]
public abstract class SlashBaseService : ApiController
{
}
產生的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 });
我也有一個TestService的在單獨的程序:
public class SampleSlashBaseService : SlashBaseService
{
[GET("TestOpenMethod")]
public string GetTestOpenMethod()
{
return "Hello anonymous!";
}
[GET("Echo/{message}")]
public string GetEcho(string message)
{
return message;
}
}
所有非常簡單的東西。問題是,當我嘗試去這個生成的網址之一時,我收到以下消息:
未找到與名爲'SampleSlashBaseService'的控制器相匹配的類型。
來自/routes.axd的路由列表也是正確的。
我做了一些相反的事情。我有控制器,它不應該是! 'defaults:new {controller =「APIController」}'應該是'defaults:new {controller =「API」}'您必須在'WebApiConfig.cs'中OMIT Controller'! – Skovy 2014-07-11 15:57:50
這就是我在WebApiConfig.cs中留下Controller後綴的情況。您的評論幫助我:) – 2015-01-21 13:08:51
我發現這件事之前拉我的頭髮。這是多麼愚蠢的錯誤。謝謝 – 2015-05-06 09:19:02