1. Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
2. 建立OWIN Startup类
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//将默认xml返回数据格式改为json
config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("datatype", "json", "application/json"));
app.UseWebApi(config);
// 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
}
3. Program中启动
static void Main(string[] args)
{
string baseAddress = "http://localhost:9000/";
Microsoft.Owin.Hosting.WebApp.Start<Startup>(url: baseAddress);
Console.WriteLine("程序已启动,按任意键退出");
Console.ReadLine();
}
4. 建立controller:ApiController
5. 配置外网访问地址