当前位置: 首页 > news >正文

C# 关于使用newlife包将webapi接口寄宿于一个控制台程序、winform程序、wpf程序运行

C# 关于使用newlife包将webapi接口寄宿于一个控制台程序、winform程序、wpf程序运行

  1. 安装newlife包
    在这里插入图片描述

  2. Program的Main()函数源码

using ConsoleApp3;
using NewLife.Log;var server = new NewLife.Http.HttpServer
{Port = 8080,Log = XTrace.Log,SessionLog = XTrace.Log
};
server.Map("/", () => "<h1>Hello NewLife!</h1></br> " + DateTime.Now.ToFullString() + "</br><img src=\"logos/leaf.webp\" />");
server.Map("/user", (String act, Int32 uid) => new { code = 0, data = $"User.{act}({uid}) success!" });
server.Map("/myHttpHandler", new MyHttpHandler());
server.MapStaticFiles("/logos", "images/");
server.MapController<MyController>("/api");server.Start();
Console.ReadLine();
  1. MyController 源码
/// <summary>
/// 控制器
/// </summary>
public class MyController
{//IHttpActionResult/// <summary>/// /// </summary>/// <param name="Code"></param>/// <param name="Name"></param>/// 请求路径:http://localhost:8080/api/GetRobotStatus?Code='code'&Name='Name'/// <returns></returns>[HttpGet]public string GetRobotStatus(string Code, string Name){try{var robotModel = new { RobotCode = "RobotCode", RobotName = "RobotName", RobotDesc = "RobotDesc", RobotRemark = "RobotRemark" };return "Successful!";}catch (Exception ex){return "Exception!";}}/// <summary>/// /// </summary>/// 请求路径:http://localhost:8080/api/GetRobotStatusJK/// <returns></returns>[HttpGet]public string GetRobotStatusJK(){try{var robotModel = new { RobotCode = "RobotCode", RobotName = "RobotName", RobotDesc = "RobotDesc", RobotRemark = "RobotRemark" };return "Successful!";}catch(Exception ex){return "Exception!";}}
}
  1. MyHttpHandler 源码
 public class MyHttpHandler : IHttpHandler{/// <summary>/// /// </summary>/// 请求路径:http://localhost:8080/myHttpHandler?name=Stone/// <param name="context"></param>public void ProcessRequest(IHttpContext context){var name = context.Parameters["name"];var html = $"<h2>你好,<span color=\"red\">{name}</span></h2>";context.Response.SetResult(html);}}
  1. 源代码百度链接
    链接:https://pan.baidu.com/s/15OxTDOBO_y5bFyrzPW3XPw?pwd=sr3c
    提取码:sr3c
http://www.lryc.cn/news/100479.html

相关文章:

  • 初识TDMQ
  • UEditor 百度富文本编辑器使用 遇到问题
  • jaeger+elasticsearch(cassandra ) 单机部署以及(400)报错
  • VSCode配置之C++ SQLite3极简配置方案
  • P5725 【深基4.习8】求三角形
  • 分布式消息中间件介绍
  • 【Linux进程篇】冯诺依曼体系
  • 陕西师范大学大学:融合传统与创新的学府之旅
  • HTML <progress> 标签
  • 常用测试工具汇总
  • 【爬虫逆向案例】某道翻译js逆向—— sign解密
  • Verilog语法学习——LV9_使用子模块实现三输入数的大小比较
  • YAML+PyYAML笔记 7 | PyYAML源码之yaml.compose_all(),yaml.load(),yaml.load_all()
  • (css)列表点击前后样式
  • Redis服务优化
  • uniAPP 浙政钉 入门手册
  • flask处理文件上传
  • 教雅川学缠论04-笔
  • Unity3d_post process layer 抗锯齿设置
  • 基于FPGA实现OSD功能
  • Java019-1——面向对象的三大特性
  • 2023年的深度学习入门指南(22) - 百川大模型13B的运行及量化
  • 无涯教程-jQuery - empty( )方法函数
  • 微信小程序实现蓝牙开锁、开门、开关、指令发送成功,但蓝牙设备毫无反应、坑
  • 微信小程序中使用echarts方法
  • 【面试题】前端中 JS 发起的请求可以暂停吗?
  • 通过社区参与解锁早期增长:Maven 远程医疗平台概览
  • Vue中使用echarts
  • 边缘计算对现代交通的重要作用
  • Python桥接模式介绍、使用