AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

栏目: ASP.NET · 发布时间: 6年前

内容简介:此项目作者IKende ,github开源地址:此人博客园地址:我基于他的github项目的 samples学习的。反正我崇拜他

此项目作者IKende ,github开源地址: https://github.com/IKende

此人博客园地址: https://www.cnblogs.com/smark/

我基于他的github项目的 samples学习的。反正我崇拜他

配置启动FastHttpApi

当前目录增加一个HttpConfig.json文件

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

设置复制

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

我看了一下源码,

源码还是很具有学习的价值的。

json对应的类HttpOptions 类,可以查看源码,看还需要哪些配置。能提取出这么多,这是学了多少东西。

以前看IdentityServer,知道.net core需要很多模块,比如静态资源处理,就需要注册一个静态资源处理,需要cookie就需要注册个cookie的类等。

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

构造函数里给了默认的值,防止你没有给json配置文件

获得文件的路径方式有点不一样,我做个笔记,因为Core可以部署在非Win的系统。所以有个文件路径分隔符的注意。

     string file = Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + mConfigFile;
            if (System.IO.File.Exists(file))
            {
                using (System.IO.StreamReader reader = new StreamReader(mConfigFile, Encoding.UTF8))
                {
                    string json = reader.ReadToEnd();
                    if (string.IsNullOrEmpty(json))
                        return new HttpOptions();
                    Newtonsoft.Json.Linq.JToken toke = (Newtonsoft.Json.Linq.JToken)Newtonsoft.Json.JsonConvert.DeserializeObject(json);
                    if (toke["HttpConfig"] != null && toke["HttpConfig"].Type == JTokenType.Object)
                    {
                        return toke["HttpConfig"].ToObject<HttpOptions>();
                    }
                    return new HttpOptions();
                }
            }
            else
            {
                return new HttpOptions();
            }

我们的配置如下,我也是从他的samples下拷贝的。

{
  "HttpConfig": {
    "Host": "",
    "Port": 5555,
    "SSL": false,
    "CertificateFile": "",
    "CertificatePassword": "",
    "MaxBodyLength": 2097152,
    "OutputStackTrace": false,
    "StaticResurceType": "xml;svg;woff;woff2;jpg;jpeg;gif;png;js;html;htm;css;txt;ico;zip;rar",
    "DefaultPage": "index.html;index.htm",
    "NotLoadFolder": "\\Files;\\Images;\\Data",
    "Manager": "admin",
    "ManagerPWD": "123456",
    "NoGzipFiles": "jpg;jpeg;png;gif;png;ico;zip;rar;bmp",
    "CacheFiles": "html;htm;js;css",
    "BufferSize": 1024,
    "WebSocketMaxRPS": 1000,
    "WriteLog": true,
    "LogToConsole": true,
    "LogLevel": "Warring",
    "FileManager": false
  }
}

运行时候已经端口5555了。

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

源码分析2

在Asp.Net MVC的Controller下都有ActionResult返回

源码下,它定义个IResult作为返回的统一,

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

然后实现个默认的ResultBase,方便我们继承,然后自己定义返回

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

Smark帮我们实现了

NotFoundResult 返回404

NoModifyResult 返回304

内部错误InnerErrorResult 500

NotSupportResult 403

UpgradeWebsocketResult 这个我不清楚,

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

TextResult 纯文本返回

JsonResult 返回ContentType=application/json 的结果

在JsonResult的基类下,加了一个ActionJsonResult  这个我不清楚

Move301Result 我不清楚

Move302Result 我不清楚

StringBytes 返回 一个 字节数组

这一次主要看Filter 这块,应该和微软的mvc的过滤器差不多吧,先看再说

我们项目建立1个Filters文件夹,建立一个AFilter类,

继承的是FilterAttribute,这个特性,一般对一个action的执行前后控制,

比如执行前 日志记录,整个action的消耗时间统计,整个权限控制,是否有权限等

/*===================================================
* 类名称: AFilter
* 类描述:
* 创建人: ay
* 创建时间: 2018/12/28 15:46:30
* 修改人: 
* 修改时间:
* 版本: @version 1.0
=====================================================*/
using BeetleX.FastHttpApi;
using System;
using System.Collections.Generic;
using System.Text;

namespace AySchoolAPI.Filters
{
    public class AFilter : FilterAttribute
    {
        public override bool Executing(ActionContext context)
        {
            Console.WriteLine(DateTime.Now + " 执行前");
            return base.Executing(context);
        }
        public override void Executed(ActionContext context)
        {
            Console.WriteLine(DateTime.Now + " 执行后");
            base.Executed(context);
        }
    }
}

跟MVC的一样,加在Controller的头上,代表一个作用域,此controller类下的action都会执行这个

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

http://127.0.0.1:5555/student/selfintroduce

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

接下来放在action上,执行时候肯定先进入filter的

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

http://127.0.0.1:5555/student/gettime

就不会进入filter 这是单独控制

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

接下来测试 指定内容执行前就返回

    public class NotFoundFilter : FilterAttribute
    {
        public override bool Executing(ActionContext context)
        {
            Console.WriteLine(DateTime.Now + "NotFoundFilter执行了");
            NotFoundResult notFound = new NotFoundResult("地址不能沉在");
            context.Result = notFound;
            return false;
        }
        public override void Executed(ActionContext context)
        {
            base.Executed(context);
        }

    }

我们用它屏蔽一个StudentController服务

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

屏蔽1个controller级别,我们也可以在action上忽略对当前action上执行的过滤器

使用SkipFilter

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

测试了下,摆放的顺序,放第一个第二个都没事

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

没有影响,做了个变态的测试

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

加自己又忽略自己,也没事哦,看来先获得action上过滤器的特性,然后再获取skip的

然后整合一个,然后判断的。

在微软的Filter,还有对于同类型的filter,Scope采取controller还是action的或者2个都执行。

还有order,多个过滤器的执行顺序

增加BFilter

    public class AFilter : FilterAttribute
    {
        public override bool Executing(ActionContext context)
        {
            Console.WriteLine(DateTime.Now + " A执行前");
            return base.Executing(context);
        }
        public override void Executed(ActionContext context)
        {
            Console.WriteLine(DateTime.Now + " A执行后");
            base.Executed(context);
        }
    }
    public class BFilter : FilterAttribute
    {
        public override bool Executing(ActionContext context)
        {
            Console.WriteLine(DateTime.Now + " B执行前");
            return base.Executing(context);
        }
        public override void Executed(ActionContext context)
        {
            Console.WriteLine(DateTime.Now + " B执行后");
            base.Executed(context);
        }
    }

开始测试

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】 测试结果 ABBA

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

修改顺序测试

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

BAAB

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

增加一个C,加在Controller上

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

测试访问结果,这个顺序对的。 按照从大到小的范围,然后从小到大的出去

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

作为过滤器,应该还有个全局过滤器,就是比Controller级别还大的,每次的访问都会经过它

,这里我们可以做 判断 请求用户身份是否合法,合法才可以继续访问

增加一个GFilter,在微软MVC中,放到Global.asax.cs 的 Application_Start注册中

这里肯定是program了

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

然后访问以下测试 GCBAABCG ,顺序正确

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

一般来说,还有MVC的过滤器 授权过滤器,异常过滤器,后面能学到再看。

接下来重点看下Context,基本上要能拿到 Action或者Controller才算过关。

AY学习基于.NET Core的web api框架-FastHttpApi【3/12】

Smark大神,给我们提供了

context参数属性

HttpContext可以获取Request和Response对象并获取或设置Cookie和Header

Parameters:得到当前请求的控制器的参数列表

Handler:得到当前控制器方法信息

Controller:得到当前方法对应的控制器对象

Result:获取或设置当前请求的返回值

Exception:获取或设置控制器处理过程的异常信息,如果存在异常的情况最终组件默认会返回500错误并带上异常信息

忘了一件事,Controller一般有个ViewBag属性,或者Action重新定向,哦,这是网页了。

这里只是Api哦,如果需要,写个父Controller类

忘了测试Controller特性是否 具有继承效果了

这里不去研究了,够用就行了。

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

整体来说,框架还不错。

推荐您阅读更多有关于“FastHttpApi,”的文章


以上所述就是小编给大家介绍的《AY学习基于.NET Core的web api框架-FastHttpApi【3/12】》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Developing Large Web Applications

Developing Large Web Applications

Kyle Loudon / Yahoo Press / 2010-3-15 / USD 34.99

As web applications grow, so do the challenges. These applications need to live up to demanding performance requirements, and be reliable around the clock every day of the year. And they need to withs......一起来看看 《Developing Large Web Applications》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具