Quando ho provato a utilizzare il WebAPI Selfhosted in LINQPad, ho continuato a ricevere lo stesso errore che un controller per la classe non esisteva.Utilizzo di WebAPI in LINQPad?
Devo creare assembly separati per WebAPI (Controllers/Classes) e quindi farli riferimento nella mia query?
Ecco il codice che sto utilizzando
#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();
}
}
Questo codice funziona bene quando in un progetto console separata in VS2012.
Ho iniziato a utilizzare AttributeRouting tramite NuGET quando non ho ottenuto il "normale" WebAPI-routing per funzionare.
L'errore ho ottenuto nel browser era: No HTTP resource was found that matches the request URI 'http://192.168.0.196:8181/players/allPlayers'.
Errore aggiuntivo: No type was found that matches the controller named 'PlayerObject'
ho prodotto una sceneggiatura LINQ con il codice (compilando le parti mancanti con alcune classi/metodi fittizio) e ha aggiunto 'config.Routes.Cast() .LogTo (Console.Out);'. Vedo il percorso 'URL: player/allPlayers GET, HEAD, OPTIONS'. Quindi sembra che il percorso sia impostato correttamente. Ricevo "Non è stato trovato alcun tipo che corrisponda al controller denominato" PlayerObject "nel browser, ma sembra diverso dal tuo errore (e probabilmente correlato al fatto che le mie classi fittizie sono troppo fittizie). –
Ho scaricato i percorsi e ho potuto vedere che sono registrati, ma quando ho provato ad andare alla pagina ho ancora ottenuto l'errore. Ho pubblicato un nuovo codice che produce l'errore. – NoLifeKing
@FrankvanPuffelen Ricevo lo stesso errore, ma solo in LINQPad, quando ho spostato questo in VS2012, ha iniziato a funzionare senza problemi. Potrei sempre iniziare a usare VS, ma la facilità di .Dump() in LINQPad vale molto per me. Poiché uso anche il mio laptop come "client" per questo webapi. :) – NoLifeKing