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
相关推荐
lsx20240614 分钟前
HTML 音频(Audio)详解
开发语言
woshihonghonga16 分钟前
【动手学深度学习】
开发语言·python
威风的虫28 分钟前
ES6 数组方法:告别循环,拥抱函数式编程
开发语言·前端·javascript
码界筑梦坊34 分钟前
240-基于Python的医疗疾病数据可视化分析系统
开发语言·python·信息可视化·数据分析·毕业设计·echarts
2301_803554521 小时前
C++ 锁类型大全详解
开发语言·c++
wuwu_q1 小时前
用通俗易懂方式,详细讲讲 Kotlin Flow 中的 map 操作符
android·开发语言·kotlin
曼巴UE51 小时前
UE5 C++ Slate 画曲线
开发语言·c++·ue5
向葭奔赴♡1 小时前
Spring IOC/DI 与 MVC 从入门到实战
java·开发语言
minji...1 小时前
C++ 面向对象三大特性之一---多态
开发语言·c++
散峰而望1 小时前
基本魔法语言函数(一)(C语言)
c语言·开发语言·编辑器·github