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

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

  1. 安装newlife包

  2. Program的Main()函数源码

csharp 复制代码
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 源码
csharp 复制代码
/// <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 源码
csharp 复制代码
 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
相关推荐
雨落倾城夏未凉5 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫6 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫7 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6257 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902117 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠8 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫10 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech10 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf12 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m62512 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#