gpt4 book ai didi

asp.net-web-api - 在 LINQPad 中使用 WebAPI?

转载 作者:行者123 更新时间:2023-12-02 20:39:40 24 4
gpt4 key购买 nike

当我尝试在 LINQPad 中使用 Selfhosted WebAPI 时,我不断收到相同的错误,即该类的 Controller 不存在。

我是否必须为 WebAPI( Controller /类)创建单独的程序集,然后在查询中引用它们?

这是我正在使用的代码

#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。

我在浏览器中遇到的错误是:未找到与请求 URI“http://192.168.0.196:8181/players/allPlayers”匹配的 HTTP 资源。

其他错误:未找到与名为“PlayerObject”的 Controller 匹配的类型

最佳答案

Web API 默认情况下会忽略非公共(public)的 Controller ,并且 LinqPad 类是嵌套的公共(public),我们在 scriptcs 中遇到了类似的问题

您必须添加自定义 Controller 解析器,这将绕过该限制,并允许您手动从执行程序集中发现 Controller 类型。

这个问题实际上已经修复了(现在 Web API Controller 只需可见而不是公开),但这发生在 9 月份,自托管的最新稳定版本是从 8 月份开始的。

所以,添加这个:

public class ControllerResolver: DefaultHttpControllerTypeResolver {

public override ICollection<Type> GetControllerTypes(IAssembliesResolver assembliesResolver) {
var types = Assembly.GetExecutingAssembly().GetExportedTypes();
return types.Where(x => typeof(System.Web.Http.Controllers.IHttpController).IsAssignableFrom(x)).ToList();
}

}

然后根据您的配置进行注册,就完成了:

var conf = new HttpSelfHostConfiguration(new Uri(address));
conf.Services.Replace(typeof(IHttpControllerTypeResolver), new ControllerResolver());

这是一个完整的工作示例,我刚刚针对 LinqPad 进行了测试。请注意,您必须以管理员身份运行 LinqPad,否则您将无法监听端口。

public class TestController: System.Web.Http.ApiController {
public string Get() {
return "Hello world!";
}
}

public class ControllerResolver: DefaultHttpControllerTypeResolver {
public override ICollection<Type> GetControllerTypes(IAssembliesResolver assembliesResolver) {
var types = Assembly.GetExecutingAssembly().GetExportedTypes();
return types.Where(x => typeof(System.Web.Http.Controllers.IHttpController).IsAssignableFrom(x)).ToList();
}
}

async Task Main() {
var address = "http://localhost:8080";
var conf = new HttpSelfHostConfiguration(new Uri(address));
conf.Services.Replace(typeof(IHttpControllerTypeResolver), new ControllerResolver());

conf.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

var server = new HttpSelfHostServer(conf);
await server.OpenAsync();

// keep the query in the 'Running' state
Util.KeepRunning();
Util.Cleanup += async delegate {
// shut down the server when the query's execution is canceled
// (for example, the Cancel button is clicked)
await server.CloseAsync();
};
}

关于asp.net-web-api - 在 LINQPad 中使用 WebAPI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14042213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com