• Owin 自寄宿 asp.net web api


    http://owin.org/

    Owin 定义了webserver和webapplication之间的标准接口,目标就是为了解耦webapplication对webserver的依赖,

    就是说以后可以轻松的建立一个轻量级的HttpServer,

    1.Basic Sample  Self Host

    下面建立一个Basic Self Host Http Server Via Owin ,全部功能就是获取客户端的Http请求,然后做出回应,当发生Unhandled Exception的时候,会自动跳转到错误页,

    Step1:Install-Package Microsoft.Owin.SelfHost

    Step2:Configuration in Startup.css

    using System;
    using System.Threading.Tasks;
    using Microsoft.Owin;
    using Owin;
    
    
    namespace OwinSelfHostBasic
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.UseErrorPage();
    
                // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
                app.Run(context => {
                    if (context.Request.Path.Value == "/fail")
                    {
                        throw new Exception("Random exception");
                    }
                    context.Response.ContentType = "text/plain";
                    return context.Response.WriteAsync("Hello, world.");
                });
            }
        }
    }
    

    app.UseErrorPage();

    为WebApplication指定错误页,

    app.Run加入响应逻辑

    Step3:指定监听端口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace OwinSelfHostBasic
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (Microsoft.Owin.Hosting.WebApp.Start<Startup>("http://localhost:9000"))
                {
                    Console.WriteLine("Press [enter] to quit...");
                    Console.ReadLine();
                }
            }
        }
    }
    

     OK,一切准备就绪,启动这个Console程序,请求 http://localhost:9000/ 这个地址,看起来比NodeJS更加方便。

     2.Self Host Web API using Owin

    Step1:Install-Package Microsoft.AspNet.WebApi.OwinSelfHost

    Step2:Startup.cs

    using Owin;
    using System.Web.Http; 
    
    namespace OwinWebAPISelfHost
    {
        public class Startup
        {
            // This code configures Web API. The Startup class is specified as a type
            // parameter in the WebApp.Start method.
            public void Configuration(IAppBuilder appBuilder)
            {
                // Configure Web API for self-host. 
                HttpConfiguration config = new HttpConfiguration();
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
    
                appBuilder.UseWebApi(config);
            }
        } 
    }
    

     Step3:APIController

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Http;
    
    namespace OwinWebAPISelfHost
    {
        public class ValuesController : ApiController
        {
            // GET api/values 
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }
        }
    }
    

     Step4:设置监听端口

    using Microsoft.Owin.Hosting;
    using System;
    using System.Net.Http;
    
    namespace OwinWebAPISelfHost
    {
        public class Program
        {
            static void Main()
            {
                string baseAddress = "http://localhost:9000/";
    
                // Start OWIN host 
                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/values").Result;
    
                    Console.WriteLine(response);
                    Console.WriteLine(response.Content.ReadAsStringAsync().Result);
    
                    Console.ReadLine();
                }
    
            }
        }
    }
    

     Step5:启动Console程序,这里我们看到返回的是Json格式的数据,当我们用火狐浏览器进行请求,会发现返回的是XML格式的数据,因为火狐浏览器默认的Accept为XML

  • 相关阅读:
    Can you answer these queries? (线段树
    小a的排列(牛客)
    Count the Colors 线段树
    Mayor's posters (离散化线段树+对lazy的理解)
    出题人的手环(求逆序对数)
    [BZOJ2251/BJWC2010]外星联络
    [ZJOI2007]报表统计
    [JLOI2016]圆的异或并
    [ZJOI2008]无序运动Movement
    [NOI2011]阿狸的打字机
  • 原文地址:https://www.cnblogs.com/LittleFeiHu/p/6193739.html
Copyright © 2020-2023  润新知