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
相关推荐
Jelena1577958579221 小时前
Java爬虫淘宝拍立淘item_search_img拍接口示例代码
开发语言·python
郝学胜-神的一滴1 天前
Python数据模型:深入解析及其对Python生态的影响
开发语言·网络·python·程序人生·性能优化
一水鉴天1 天前
整体设计 定稿 之26 重构和改造现有程序结构 之2 (codebuddy)
开发语言·人工智能·重构·架构
小二·1 天前
MyBatis基础入门《十五》分布式事务实战:Seata + MyBatis 实现跨服务数据一致性
分布式·wpf·mybatis
star _chen1 天前
C++ std::move()详解:从小白到高手
开发语言·c++
lzhdim1 天前
C#开发者必知的100个黑科技(前50)!从主构造函数到源生成器全面掌握
开发语言·科技·c#
刺客xs1 天前
Qt----事件简述
开发语言·qt
程序员-King.1 天前
【Qt开源项目】— ModbusScope-进度规划
开发语言·qt
syt_10131 天前
Object.defineProperty和Proxy实现拦截的区别
开发语言·前端·javascript
liu****1 天前
Python 基础语法(二):程序流程控制
开发语言·python·python基础