您的位置:首页 > 编程语言 > ASP

Use OWIN to Self-Host ASP.NET Web API 2 来访问我的webapi

2017-04-01 17:19 543 查看
就是说我们本地的http://localhost:49708/api/test可以通过 这个东西来访问(懒得挂载iis,当然它的强大可不这些,由于测试出了问题 出记录一下)

首先去Nuget包里找到Microsoft.AspNet.WebApi.OwinSelfHost这个东西

然后创建一个控制台程序

class Program
{
static void Main(string[] args)
{
try
{
string baseAddress = "http://localhost:49708/";

// Start OWIN host 这段注释是官网提供的代码怎么弄都用错,下面没打注释的是可以运行的
              //地址:https://docs.microsoft.com/en-us/aspnet/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api
//using (WebApp.Start<Startup>(url: baseAddress))
//{
//    // Create HttpCient and make a request to api/values
//    HttpClient client = new HttpClient();

//    var response = client.GetAsync(baseAddress + "api/test").Result;

//    Console.WriteLine(response);
//    Console.WriteLine(response.Content.ReadAsStringAsync().Result);
//    Console.ReadLine();
//}

HttpClient client = new HttpClient();

var response = client.GetAsync(baseAddress + "api/test").Result;

Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}

}
}
//资料参考:http://www.cnblogs.com/TianFang/p/3728760.html
//下面就是webapi的响应信息


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: