当我尝试在 LINQPad 中使用 Selfhosted WebAPI 时,我一直收到相同的错误,即该类的控制器不存在。
我是否必须为 WebAPI(控制器/类)创建单独的程序集,然后在查询中引用它们?
这是我正在使用的代码
#region namespaces
using AttributeRouting;
using AttributeRouting.Web.Http;
using AttributeRouting.Web.Http.SelfHost;
using System.Web.Http.SelfHost;
using System.Web.Http.Routing;
using System.Web.Http;
#endregion
public void Main()
{
var config = new HttpSelfHostConfiguration("http://192.168.0.196:8181/");
config.Routes.MapHttpAttributeRoutes(cfg =>
{
cfg.AddRoutesFromAssembly(Assembly.GetExecutingAssembly());
});
config.Routes.Cast<HttpRoute>().Dump();
AllObjects.Add(new UserQuery.PlayerObject { Type = 1, BaseAddress = "Hej" });
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
using(HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Server open, press enter to quit");
Console.ReadLine();
server.CloseAsync();
}
}
public static List<PlayerObject> AllObjects = new List<PlayerObject>();
public class PlayerObject
{
public uint Type { get; set; }
public string BaseAddress { get; set; }
}
[RoutePrefix("players")]
public class PlayerObjectController : System.Web.Http.ApiController
{
[GET("allPlayers")]
public IEnumerable<PlayerObject> GetAllPlayerObjects()
{
var players = (from p in AllObjects
where p.Type == 1
select p);
return players.ToList();
}
}
此代码在 VS2012 的单独控制台项目中工作正常。
当我没有让“正常”WebAPI 路由工作时,我开始通过 NuGET 使用 AttributeRouting。
我在浏览器中遇到的错误是:No HTTP resource was found that matches the request URI 'http://192.168.0.196:8181/players/allPlayers'.
附加错误:No type was found that matches the controller named 'PlayerObject'