XIKEW.COM - 实用教程 - (1)LinkCore插件开发之路由设置 - 实用教程, - 路由设置可以快速对接WebApi类型的项目,通过简单的继承就能完成

[已过时] (1)LinkCore插件开发之路由设置
LINKCORE NETCORE 4/17/2020 11:30:38 AM 阅读:6

路由设置可以快速对接WebApi类型的项目,通过简单的继承就能完成

注意

LINKCORE已经支持到 NET5.0+ 本文章只使用NET3.0

LinkCore?

点击查看介绍

提前准备

  1. 新建一个NetCore 3.1项目
  2. 引入WebMain根目录下的LinkCore.Interface.dll (所有插件都需要引入)
  3. 引入WebMain根目录下的LinkCore.Interface.Router.dll Web项目必须引入路由接口

Hello World

namespace Demo
{
    class V1 : Router
    {
        public V1(){
            //这里可以初始化数据库连接操作
            //可以添加路由注册
        }
        
        //如果不想开放该方法可以设置为 Private 访问级
        public Index() {
            ResponseWriteBody("Hello World!")
        }
    }
}

发布

1.将代码发布到 WebMain/plugins/Demo/Demo.dll

接下来可以根据路由/Demo/V1/Index访问并且看到浏览器输出 Hello World!

路由注册

/Demo/V1/Index 默认会进入Index方法, 但有时候这不是我们想要的

我们可以在构造函数里添加路由注册,当然前提是该方法是一个 Public

RouterRegister("/", this.Other);

同样我们也可以直接使用匿名函数来完成简单的工作

RouterRegister("/", () => {
    //TODO
});

获取数据

//数据类型是application/json模式下
var json = JSON.Parse(RequestReadBody());
Console.WriteLine(json.xxx);

说明:推荐使用RequestReadBody,因为默认处理了 Context.Request.Body 多次获取的问题

页面输出

ResponseWriteBody 可以帮我们完成接口返回的工作

ResponseWriteBody("Hello World!")

获取所有输出数据

因为 Context.Response.WriteAsync 方法可以多次调用,如果想获取所有输出到也没的数据可以使用 Router.ResponseBody 但是请注意:该方法只能获取当前 Router 实例部分数据

//this 当前类为Router的继承类
Console.WriteLine(this.ResponseBody);

ResponseWriteHeader 可以修改服务器返回的头部信息 所以习惯使用Json格式返回数据接口的话还可以增加头部 Content-Type

ResponseWriteHeader("Content-Type", "application/json;charset=utf-8");
ResponseWriteHeader("Server", "LC/1.0");

更高级的使用方法

Context

Router内置了Context的对象,该对象继承了HttpContext的方法

路由映射

如果我们想为自己的插件做一个长远的打算,很多时候我们会通过一个入口连接WebMain,然后通往自己的小世界. 我想路由映射功能是能帮上忙的

我们的项目结构是这样的

.
├── Demo
    ├── Controller     //namespace Demo.Controller
        ├── User.cs
        ├── Group.cs
        └── ...

我们可以通过路由映射的方法快速对接到我们想要的 namespace


namespace Demo
{
    class V1 : Router
    {
        public V1(){
            RouterRegister("/Test", this.Index);
        }
        
        public Index() {
            ResponseWriteBody("Hello World!")
            RouterMap(Assembly.GetExecutingAssembly(), "Controller", className: "User", methodName:"Index");
        }
    }
}

这个时候我们就可以使用/Demo/V1/Test/Index 路由到 Controller.User 控制器 Index 方法了

插件内部调用

RunByPlugin(pluginMethod:"PluginName.Controller.User", data:"123456", headers: null, resp => {
    //返回ResponseWriteBody输出的内容
})

路由错误捕捉模式

可以捕捉自定义 LCException 更友好的接口返回处理

CatchRouterException(() =>
{
    RouterMap(Assembly.GetExecutingAssembly(), "Controller");
}, (ex, isLcEx) =>
{
    if (isLcEx)
    {
        ResponseWriteBody(Utiliy.OutPut("0400", message: ex.Message));
    }
    else
    {
        Logger.Error(ex.ToString());
        ResponseWriteBody(Utiliy.OutPut("0500", message: ex.Message));
    }
});

小贴士

注意 LinkCore.Interface.Router 依赖于 Microsoft.AspNetCore.Http.Abstractions 2.2 版本,最好使用 Nuget 来管理引用。避免与 AspNetCoreMicrosoft.AspNetCore.Http.Abstractions 3.1.0 版本冲突!

使用Microsoft Visual Studio开发的同学要注意几个问题

  • WebMain 默认启动了 响应压缩中间件 UseResponseCompression UseSession UseWebSockets
  • Demo.csproj配置 应用程序 > 输出类型 > 类库
  • 程序洁癖者推荐 : 生成事件 > 生成后事件命令行
cd $(OutDir)
del /f /s /q LinkCore.*
del /f /s /q Newtonsoft.Json.dll
del /f /s /q *.json
del /f /s /q *.pdb
  • 可以代码模式下修改Demo.csproj输出路径 AppendTargetFrameworkToOutputPath !很重要 :)
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <OutputPath>路径WebLinkCorepluginsDemo</OutputPath>
</PropertyGroup>