内容简介:此项目作者IKende ,github开源地址:此人博客园地址:我基于他的github项目的 samples学习的。反正我崇拜他
此项目作者IKende ,github开源地址: https://github.com/IKende
此人博客园地址: https://www.cnblogs.com/smark/
我基于他的github项目的 samples学习的。反正我崇拜他
我新建一个.NET Core 控制台AySchoolAPI,表示一个学校的API服务,然后引用Nuget搜fasthttpapi
然后安装
把这个也安装了,顺便也学习这个。
创建了一个文件夹,建立一个普通类(自己以前用过owin搭建过,所以学起来应该简单)
/*=================================================== * 类名称: StudentApi * 类描述: * 创建人: ay * 创建时间: 2018/12/28 9:23:45 * 修改人: * 修改时间: * 版本: @version 1.0 =====================================================*/ using System; using System.Collections.Generic; using System.Text; namespace AySchoolAPI.Student { [BeetleX.FastHttpApi.Controller] public class StudentController { public object SelfIntroduce(string name) { return new { Hello = "我叫 " + name, InSchoolTime = new DateTime(1997,9,1,10,0,0) }; } } }
然后Program.cs
using System; using AySchoolAPI.Student; using BeetleX.FastHttpApi; using BeetleX.FastHttpApi.Data; namespace AySchoolAPI { class Program { private static BeetleX.FastHttpApi.HttpApiServer mApiServer; static void Main(string[] args) { mApiServer = new BeetleX.FastHttpApi.HttpApiServer(); mApiServer.Debug(); mApiServer.Register(typeof(Program).Assembly); mApiServer.Open(); Console.Write(mApiServer.BaseServer); Console.Read(); Console.ReadLine(); } } }
运行项目,我们这里分了文件夹放Controller
在.net mvc或者api中,是Name+Controller组成类名,通过文件夹名/Name/ActionName访问操作的,
而FastHttpApi中,是不区分的。运行项目,然后用postman测试,反而用微软的mvc规则访问不了.
当action有参数,你不传入也是可以的
肯定这样不规范的,因为同名的Action还是有的
接下来,在StudentController上加上
[BeetleX.FastHttpApi.Controller(BaseUrl = "Student/", SingleInstance = true)]
竟然还可以控制是否单例
很好的设计
暂时没发现 默认的Get方法的功能,没必要也是对的。
(题外话:设计的比微软的自带的方便多了)
题外话:
估计如果的controller写在其他类库,需要注册下。
我的强迫症,新建了一个Core类库,然后引用FastHttpApi,然后启动的控制台引用下
/*=================================================== * 类名称: StudentApi * 类描述: * 创建人: ay * 创建时间: 2018/12/28 9:23:45 * 修改人: * 修改时间: * 版本: @version 1.0 =====================================================*/ using System; using System.Collections.Generic; using System.Text; namespace EducationAPI { [BeetleX.FastHttpApi.Controller(BaseUrl = "Edu/", SingleInstance = true)] public class EducationController { public object Say(string word) { return "我啊,你说:"+ word; } } }
真的可以了
看这使用美如画,本想学习总结下。奈何本人没文化,一句卧槽闯天下。
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
事件2
我在sample里看到这些代码,假设你学过.net webapi了,我就跳过很多东西了
/*=================================================== * 类名称: StudentApi * 类描述: * 创建人: ay * 创建时间: 2018/12/28 9:23:45 * 修改人: * 修改时间: * 版本: @version 1.0 =====================================================*/ using BeetleX.FastHttpApi; using BeetleX.FastHttpApi.Data; using System; using System.Collections.Generic; using System.Text; namespace AySchoolAPI.Student { [BeetleX.FastHttpApi.Controller(BaseUrl = "Student/", SingleInstance = true)] public class StudentController { public object SelfIntroduce(string name) { return new { Hello = "我叫 " + name, InSchoolTime = new DateTime(1997,9,1,10,0,0) }; } public object GetTime() { return DateTime.Now; } // Get /hello?name=henry // or // Get /hello/henry [Get(Route = "{name}")] public object Hello(string name, IHttpContext context) { return $"hello {name} {DateTime.Now}"; } [Get(Route = "{id}-{value}")] public object SetValue(string id, string value, IHttpContext context) { return $"{id}={value} {DateTime.Now}"; } [Post] [NoDataConvert] public object PostStream(IHttpContext context) { Console.WriteLine(context.Data); string value = context.Request.Stream.ReadString(context.Request.Length); return value; } //json {"name":"","value":""} [Post] [JsonDataConvert] public object Post(string name, string value, IHttpContext context) { Console.WriteLine(context.Data); return $"{name}={value}"; } //name=aaa&value=aaa [Post] [FormUrlDataConvert] public object PostForm(string name, string value, IHttpContext context) { Console.WriteLine(context.Data); return $"{name}={value}"; } } }
这里是wcf的rest风格的路由模板,最后IHttpContext理解为上下文,这里能自动绑定值,应该ModelBinderProvider和ModelBinder的实现,
不知道能不能放个类,然后自动属性对应上就赋值了。貌似IHttpContext.Data属性有键值对。
启动项目,由于开启了日志
mApiServer.Options.LogLevel = BeetleX.EventArgs.LogType.Warring; mApiServer.Options.LogToConsole = true;
上面两个Get没啥好讲的。
有个特性NoDataConvert
有些场需要读取数据流来转换自定义的数据,这种方式组件可以这样按以下方式来做
加上这个
发现是空了
接下来去掉这个
但是下面的那行代码报错了
不管了。
这个特性
smark在博客说
组件默认是从Context-Type来分析转换器,为了避免请求不带这个类型或设置错误的情况可以在控制器或方法上加个JsonDataConvert来明确数据解析方式。
看post的数据怎么接收,我们就 2个键值,或者说1个类,就2个属性
方式1
就是上面说的,从context拿键值
方式2,在action的参数上,如上图
我们看到有很多 ,这里选择json
在Headers中,Content-Type是application/json,作者根据这个加载解析器的。
方式3
新建一个类,包含需要的输入参数,然后action的第一个参数是那个类,变量名要是body,不能乱取
public class Post2Property { public string name { get; set; } public string value { get; set; } }
[Post] [JsonDataConvert] public object Post2(Post2Property body, IHttpContext context) { Console.WriteLine(context.Data); return $"{body.name}={body.value}"; }
由于web开发,有个dom叫Form,它也有提交
提交的body类型 x-www-form-urlencoded
在Smark中也加了支持 [FormUrlDataConvert]
[Post] [FormUrlDataConvert] public object PostForm(string name, string value, IHttpContext context) { Console.WriteLine(context.Data); return $"{name}={value}"; }
接下来测试混搭
[Post] [FormUrlDataConvert] public object PostForm2(string other,string name, string value, IHttpContext context) { Console.WriteLine(context.Data); return $"{name}={value}={other}"; }
这种写法不行哦。
[Post] [FormUrlDataConvert] public object PostForm3(Post2Property body, string other, IHttpContext context) { Console.WriteLine(context.Data); return $"{body.name}={body.value}={other}"; }
上传文件
[Post] [MultiDataConvert] public object UploadFile(string remark, IHttpContext context) { foreach (var file in context.Request.Files) using (System.IO.Stream stream = System.IO.File.Create(file.FileName)) { file.Data.CopyTo(stream); } return $"{DateTime.Now} {remark} {string.Join(",", (from fs in context.Request.Files select fs.FileName).ToArray())}"; } [Post] [MultiDataConvert] public object UploadFile(string remark, PostFile file) { using (System.IO.Stream stream = System.IO.File.Create(file.FileName)) { file.Data.CopyTo(stream); } return $"{DateTime.Now} {remark} {file.FileName}"; }
新建一个网页
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <form method="post" action="127.0.0.1:9090/Student/UploadFile" enctype="multipart/form-data"> <input type="text" name="name" /> <input type="file" name="file" multiple /> <input type="submit" /> </form> </body> </html>
放入iis的根目录
已经可以拿到文件流,然后你可以存起来了。
把下面一个注释掉,就可以进入到上一个了。
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
目前为止,作为一个web服务器很符合规定了。还有安全性,权限,异常等东西了。
推荐您阅读更多有关于“FastHttpApi,”的文章
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
MD5 加密
MD5 加密工具
HEX CMYK 转换工具
HEX CMYK 互转工具