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
相关推荐
爱编程的鱼9 分钟前
OpenCV Python 绑定:原理与实战
c语言·开发语言·c++·python
sdgsdgdsgc1 小时前
Next.js企业级应用开发:SSR、ISR与性能监控方案
开发语言·前端·javascript
rit84324995 小时前
基于MATLAB的模糊图像复原
开发语言·matlab
fie88895 小时前
基于MATLAB的声呐图像特征提取与显示
开发语言·人工智能
_extraordinary_6 小时前
Java SpringMVC(二) --- 响应,综合性练习
java·开发语言
CsharpDev-奶豆哥6 小时前
ASP.NET中for和foreach使用指南
windows·microsoft·c#·asp.net·.net
@。1247 小时前
对于灰度发布(金丝雀发布)的了解
开发语言·前端
hsjkdhs8 小时前
C++之多层继承、多源继承、菱形继承
开发语言·c++·算法
Full Stack Developme8 小时前
Python Redis 教程
开发语言·redis·python
The Sheep 20239 小时前
WPF自定义路由事件
大数据·hadoop·wpf