[英]HttpRequestMessage not registered in Autofac with in memory hosting of Web API service
[英]Programmatically send an HttpRequestMessage to the Web API pipeline without Hosting
我想知道是否有一种方法可以手动制作HTTPRequestMessage对象并将其以编程方式“发送”到Web API管道,而无需实际将Web API项目托管在某个地方?
以下文章中描述的内存中测试是如何实现此目的的示例:
请注意,这是一则旧文章,但是大多数概念仍然相关。
您可以创建一个HttpServer ,然后将其传递给HttpClient构造函数。 请参见下面的代码片段:
HttpConfiguration configuration = new HttpConfiguration();
configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var server = new HttpServer(configuration);
var client = new HttpClient(server);
Task<HttpResponseMessage> task = client.GetAsync("http://www.whatever/yourcontroller");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.