zoukankan      html  css  js  c++  java
  • PCB 脱离IIS的Web应用

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

    一.NuGet 下载Owin

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

    三.代码写一个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

      

           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();
            }

  • 相关阅读:
    hdu5360 Hiking(水题)
    hdu5348 MZL's endless loop(欧拉回路)
    hdu5351 MZL's Border(规律题,java)
    hdu5347 MZL's chemistry(打表)
    hdu5344 MZL's xor(水题)
    hdu5338 ZZX and Permutations(贪心、线段树)
    hdu 5325 Crazy Bobo (树形dp)
    hdu5323 Solve this interesting problem(爆搜)
    hdu5322 Hope(dp)
    Lightoj1009 Back to Underworld(带权并查集)
  • 原文地址:https://www.cnblogs.com/pcbren/p/9926276.html
Copyright © 2011-2022 走看看