1.创建web Api项目
2.创建web项目
3.运行项目正常显示
4.右键项目名-发布
5.点击文件夹
6.发布位置选择iis下的文件目录
7.点击发布
8.IIS配置(我的电脑-右键-管理-服务和应用程序)
9.添加应用程序池(选择无代码托管 集成)
10.配置路径添加应用池
11.访问加上接口名(http://172.168.1.164:8888/weatherforecast)
12.会生成一个json格式的文件
13.c#调用接口
private void button1_Click(object sender, EventArgs e)
{
string url = "http://172.168.1.164:8888/WeatherForecast";
var str = HttpGet(url);
JavaScriptSerializer js = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
List<WeatherForecast> list = js.Deserialize<List<WeatherForecast>>(str);
string result = list[0].Summary;
}
public static string HttpGet(string url)
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Accept = "json/html, application/xhtml+xml, */*";
request.ContentType = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
}