PCB 脱离IIS的Web应用

在用.net Web编程中,我们写好的Web应用首选会挂在IIS上面,因为它足稳定并且功能齐全,但这不是我们唯一的选择,微软给我们提供了Owin组件,Web应该的宿主可以不再是IIS了,有了Owin后,宿主可以是控制台,也可以是Windows服务上;这样挺爽的。因为本公司另一个APS系统没挂在IIS上面,这里将它的方法分享一下.但我个人还是更倾向于挂在IIS上面,感觉更靠谱些.

一.NuGet 下载Owin

PCB 脱离IIS的Web应用

二.安装完后,引用增加下图如下dll

PCB 脱离IIS的Web应用

三.代码写一个WebAPI接口例子并启动

1.新建类:ppeflowController  写get方法

    public class ppeflowController : ApiController
{ public IEnumerable<string> Get()
{
return new string[] { "开料", "钻孔", "沉铜", "板镀" };
}
}

2.新建类:Startup 并写WebAPI路由配置

    public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "pcbrenApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "ppeflow", action = "Get", id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
}
}

3.在Main方法中写启动Web代码

      static void Main(string[] args)
{
string baseAddress = "http://localhost:8989/";
WebApp.Start<Startup>(baseAddress);
Console.ReadLine();
}

四.调用WebAPI

1.网页访问:http://localhost:8989/api

PCB 脱离IIS的Web应用

2.C#调用WebAPI

  static void Main(string[] args)
{
string baseAddress = "http://localhost:8989/";//读取WEB API
HttpClient client = new HttpClient();
var response = client.GetAsync(baseAddress + "api/ppeflow").Result;
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.ReadLine();
}

PCB 脱离IIS的Web应用

上一篇:通过代码设置button中文字的对齐方式


下一篇:[C#]反射遍历对象属性