C# SelfHost WebAPI (2)

76 篇文章 1 订阅

上一个自主托管Web Api 的示例存储一些问题,如下Self Host Web Api 测试OK。

1. 创建 ConsoleApp

定义项目名称: SefHostWebApiSample

2. 添加第三方组件

Microsoft.AspNet.WebApi.SelfHost

Microsoft.Net.Http

Newtonsoft.Json

在这里插入图片描述

3. 创建Controller 类 ,ValuesController.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Net;

using System.Net.Http;

using System.Web.Http;

namespace SefHostWebApiSample

{

public class ValuesController : ApiController

{

    // GET api/values

    public IEnumerable<string> Get()

    {

        return new string[] { "value1", "value2", "value3" };

    }

    // GET api/values/5

    public string Get(int id)

    {

        return "value" +id;

    }

    // POST api/values

    public void Post([FromBody] string value)

    {

    }

    // PUT api/values/5

    public void Put(int id,[FromBody] string value)

    {

    }

    // DELETE api/values/5

    public void Delete(int id)

    {

    }

}

}

4. Program.cs 中添加SelfHost代码

using System.Web.Http.SelfHost;

using System.Web.Http;

namespace SefHostWebApiSample

{

class Program

{

    static void Main(string[] args)

    {

        Uri myUri = new Uri(@"http://localhost:9999");

        // Let have our configurations readY

        HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(myUri);

        // configure routes

        config.Routes.MapHttpRoute(

            name: "DefaultApi",

            routeTemplate: "api/{controller}/{id}",

            defaults: new { id = RouteParameter.Optional }

        );

        HttpSelfHostServer server = new HttpSelfHostServer(config);

        // Start listening

        server.OpenAsync().Wait();

        Console.WriteLine("WebApi hosted on " + myUri.AbsoluteUri + " It can be tested now");

        Console.ReadLine();

    }

}

}

5. 运行App

App显示如下信息:

WebApi hosted on http://localhost:9999/ It can be tested now

在浏览器上输入:http://localhost:9999/api/values

This XML file does not appear to have any style information associated with it. The document tree is shown below.

value1

value2

value3

在浏览器上输入:http://localhost:9999/api/values/5

This XML file does not appear to have any style information associated with it. The document tree is shown
value5

测试OK !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
对于C# WinForms应用程序中建立Web API服务器,你可以按照以下步骤进行操作: 1. 首先,确保你的C# WinForms项目已经创建并正常运行。 2. 在项目中添加一个新的类文件(.cs),该文件将包含Web API的控制器代码。 3. 在控制器类中,使用System.Web.Http命名空间并继承ApiController类。 4. 在控制器类中,创建要公开的API方法。你可以使用各种HTTP动词(如GET、POST、PUT、DELETE等)来处理不同的API请求。 5. 在控制器类中,使用[Route]属性来定义API方法的路由。这将决定API方法可以通过哪个URL进行访问。 6. 在控制器类中,使用[HttpGet]、[HttpPost]等属性来定义API方法的HTTP动词。 7. 在控制器类中,实现API方法的具体逻辑。这可能包括从数据库中检索数据、处理请求参数等。 8. 在WinForms应用程序的入口点(例如Main函数)中,使用System.Web.Http.SelfHost命名空间来启动Web API服务器。 9. 在Web API服务器的启动代码中,使用HttpSelfHostConfiguration类来配置服务器设置,例如指定要监听的端口号、启用跨域访问等。 10. 在Web API服务器的启动代码中,使用HttpSelfHostServer类来创建并启动服务器。 11. 运行你的C# WinForms应用程序,并确保Web API服务器已经成功启动。 现在,你的C# WinForms应用程序中就有一个Web API服务器了,可以通过API方法来处理HTTP请求。你可以使用工具(例如Postman)来测试和调试API方法。请记住,这只是一个基本的示例,你可以根据自己的需求进行更多的定制化和功能扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值