【发布时间】:2014-05-14 08:17:20
【问题描述】:
我正在尝试像这里一样使用 owin 运行一个自托管的 Web api 应用程序
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
public class Program
{
static void Main()
{
string baseAddress = "http://localhost:9000/";
// Start OWIN host
using (WebApp.Start<Startup>(url: baseAddress))
{
Console.WriteLine("App started");
Console.ReadLine();
}
}
}
public class Startup
{
// This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
}
}
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
哪个事件。
我尝试通过 nuget 安装 Microsoft.AspNet.WebApi (Web Api 2.1),但由于框架 4.5 的要求而失败,所以我安装了 AspNetWebApi (Web Api)。
但是我在任何地方都找不到扩展方法UseWebApi。我是否必须安装另一个软件包,或者是否无法使用 Framework 4 托管 Web api?
【问题讨论】:
-
我不是专家,我自己才刚刚开始使用 WebApi 和 OWIN,但是由于 Web Api 2 是完全异步的,它是否可以在不依赖 Microsoft.Bcl.Async 的情况下在 .NET 4 上使用?
-
Well WebApi (V1) 还有一个位于
System.Web.Http.dll v4.0的ApiController 类 -
WebApi (V1) 可以使用
HttpSelfHostServer (Microsoft.AspNet.WebApi.SelfHost package)自托管,但我正在寻找 Owin 实现,因为我已经使用 owin 托管了 Nancyfx。
标签: c# .net asp.net-web-api