OWIN Middleware开发入门

Program.cs

using Microsoft.Owin;
using Microsoft.Owin.Hosting;
using System;

namespace OWINDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var baseAddress = "http://localhost:9001";
            WebApp.Start<Startup>(url: baseAddress);
            Console.WriteLine("exit by any key");
            Console.ReadLine();
        }
    }
}

Startup.cs

using Owin;
using System.Web.Http;

namespace OWINDemo
{
    public class Startup
    {
        public void Configuration(IAppBuilder appBuilder)
        {
            var config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name:"DefaultApi",
                routeTemplate:"api/{controller}/{id}",
                defaults:new {id=RouteParameter.Optional}
                );
            appBuilder.UseWebApi(config);
            appBuilder.Use<SampleMiddleware>("[自定义可选的object参数]");
        }
    }
}

BlogController.cs

using System.Collections.Generic;
using System.Web.Http;

namespace OWINDemo
{
    public class BlogController: ApiController
    {
        string[] app=new string[] { "南通", "开发区"};
        public IEnumerable<string> Get()
        {
            return app;
        }
        public string Get(int id)
        {
            return $"owin {app[id]} by:wu";
        }
        public void Post([FromBody]string value)
        {
            app[0] = "valuePost";
        }
        public void Put(int id,[FromBody]string value)
        {
            app[id] = "Put";
        }
        public void Delete(int id)
        {
            app[id] = "null";
        }
    }
}

SampleMiddleware.cs

using System;
using System.Threading.Tasks;
using Microsoft.Owin;

namespace OWINDemo
{
    public class SampleMiddleware:OwinMiddleware
    {
        private object _mOptions;


        /// <summary>
        /// 构造函数,第一个参数必须为 OwinMiddleware对象 ;第一个参数是固定的,后边还可以添加自定义的其它参数
        /// </summary>
        /// <param name="next">下一个中间件</param>
        public SampleMiddleware(OwinMiddleware next,object options) 
            : base(next)
        {
            _mOptions = options;
        }


        /// <summary>
        /// 处理用户请求的具体方法,该方法是必须的
        /// </summary>
        /// <param name="context">OwinContext对象</param>
        /// <returns></returns>
        public override Task Invoke(IOwinContext context)
        {
            PathString tickPath = new PathString("/tick");
            //判断Request路径为/tick开头
            if (context.Request.Path.StartsWithSegments(tickPath))
            {
                string content = DateTime.Now.Ticks.ToString();
                //输出答案--当前的Tick数字
                context.Response.ContentType = "text/plain";
                context.Response.ContentLength = content.Length;
                context.Response.StatusCode = 200;
                context.Response.Expires = DateTimeOffset.Now;
                context.Response.Write(content);
                //解答者告诉Server解答已经完毕,后续Middleware不需要处理
                return Task.FromResult(0);
            }
            else
                //中间件的实现代码
                return Next.Invoke(context);
        }
    }
}

运行结果如图:

这里写图片描述

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值